Задать вопрос

Почему uasort игнорирует вложенный массив при сортировке?

При сортировке не видит children, нужно отсортировать весь массив по ключу ncount, по убыванию.
Очень нужна помощь.

Код
$array = array(
	151 =>
	array(
		'id' => '151',
		'ncount' => 0,
	),
	34 =>
	array(
		'id' => '34',
		'ncount' => 47134,
		'children' =>
		array(
			66 =>
			array(
				'id' => '66',
				'ncount' => 7168,
			),
			67 =>
			array(
				'id' => '67',
				'ncount' => 2509,
			),
			68 =>
			array(
				'id' => '68',
				'ncount' => 3726,
			),
			69 =>
			array(
				'id' => '69',
				'ncount' => 2364,
			),
			70 =>
			array(
				'id' => '70',
				'ncount' => 62,
			),
			71 =>
			array(
				'id' => '71',
				'ncount' => 23628,
			),
			72 =>
			array(
				'id' => '72',
				'ncount' => 2000,
			),
			73 =>
			array(
				'id' => '73',
				'ncount' => 13705,
			),
			74 =>
			array(
				'id' => '74',
				'ncount' => 6214,
			),
			75 =>
			array(
				'id' => '75',
				'ncount' => 8710,
			),
			76 =>
			array(
				'id' => '76',
				'ncount' => 585,
			),
			77 =>
			array(
				'id' => '77',
				'ncount' => 41884,
			),
			78 =>
			array(
				'id' => '78',
				'ncount' => 0,
			),
			79 =>
			array(
				'id' => '79',
				'ncount' => 3489,
			),
			80 =>
			array(
				'id' => '80',
				'ncount' => 1634,
			),
			81 =>
			array(
				'id' => '81',
				'ncount' => 6218,
			),
			82 =>
			array(
				'id' => '82',
				'ncount' => 1838,
			),
			83 =>
			array(
				'id' => '83',
				'ncount' => 1164,
			),
			84 =>
			array(
				'id' => '84',
				'ncount' => 963,
			),
			85 =>
			array(
				'id' => '85',
				'ncount' => 909,
			),
			86 =>
			array(
				'id' => '86',
				'ncount' => 3599,
			),
			87 =>
			array(
				'id' => '87',
				'ncount' => 3044,
			),
			88 =>
			array(
				'id' => '88',
				'ncount' => 2720,
			),
			89 =>
			array(
				'id' => '89',
				'ncount' => 10233,
			),
			131 =>
			array(
				'id' => '131',
				'ncount' => 1659,
			),
			132 =>
			array(
				'id' => '132',
				'ncount' => 123,
			),
			133 =>
			array(
				'id' => '133',
				'ncount' => 0,
			),
			148 =>
			array(
				'id' => '148',
				'ncount' => 1101,
			),
			127 =>
			array(
				'id' => '127',
				'ncount' => 4025,
			),
			128 =>
			array(
				'id' => '128',
				'ncount' => 4177,
			),
			129 =>
			array(
				'id' => '129',
				'ncount' => 4401,
			),
			130 =>
			array(
				'id' => '130',
				'ncount' => 2944,
			),
		),
	),
	124 =>
	array(
		'id' => '124',
		'ncount' => 650,
		array(
			125 =>
			array(
				'id' => '125',
				'ncount' => 239,
			),
			126 =>
			array(
				'id' => '126',
				'ncount' => 410,
			),
		),
	),
	35 =>
	array(
		'id' => '35',
		'ncount' => 15148,
		'children' =>
		array(
			90 =>
			array(
				'id' => '90',
				'ncount' => 371,
			),
			91 =>
			array(
				'id' => '91',
				'ncount' => 3000,
			),
			92 =>
			array(
				'id' => '92',
				'ncount' => 0,
			),
			93 =>
			array(
				'id' => '93',
				'ncount' => 908,
			),
			94 =>
			array(
				'id' => '94',
				'ncount' => 0,
			),
			95 =>
			array(
				'id' => '95',
				'ncount' => 4317,
			),
			96 =>
			array(
				'id' => '96',
				'ncount' => 523,
			),
			97 =>
			array(
				'id' => '97',
				'ncount' => 896,
			),
			98 =>
			array(
				'id' => '98',
				'ncount' => 1381,
			),
			99 =>
			array(
				'id' => '99',
				'ncount' => 14,
			),
			100 =>
			array(
				'id' => '100',
				'ncount' => 6648,
			),
			101 =>
			array(
				'id' => '101',
				'ncount' => 3252,
			),
			102 =>
			array(
				'id' => '102',
				'ncount' => 855,
			),
			103 =>
			array(
				'id' => '103',
				'ncount' => 366,
			),
			104 =>
			array(
				'id' => '104',
				'ncount' => 389,
			),
			105 =>
			array(
				'id' => '105',
				'ncount' => 482,
			),
			106 =>
			array(
				'id' => '106',
				'ncount' => 551,
			),
			107 =>
			array(
				'id' => '107',
				'ncount' => 9256,
			),
			108 =>
			array(
				'id' => '108',
				'ncount' => 2804,
			),
			109 =>
			array(
				'id' => '109',
				'ncount' => 4204,
			),
			110 =>
			array(
				'id' => '110',
				'ncount' => 2199,
			),
			111 =>
			array(
				'id' => '111',
				'ncount' => 834,
			),
			123 =>
			array(
				'id' => '123',
				'ncount' => 1449,
			),
			134 =>
			array(
				'id' => '134',
				'ncount' => 1104,
			),
			135 =>
			array(
				'id' => '135',
				'ncount' => 1166,
			),
			136 =>
			array(
				'id' => '136',
				'ncount' => 1153,
			),
			137 =>
			array(
				'id' => '137',
				'ncount' => 878,
			),
			138 =>
			array(
				'id' => '138',
				'ncount' => 756,
			),
			139 =>
			array(
				'id' => '139',
				'ncount' => 107,
			),
			140 =>
			array(
				'id' => '140',
				'ncount' => 0,
			),
			149 =>
			array(
				'id' => '149',
				'ncount' => 170,
			),
		),
	),
	53 =>
	array(
		'id' => '53',
		'ncount' => 2740,
		'children' =>
		array(
			112 =>
			array(
				'id' => '112',
				'ncount' => 0,
			),
			113 =>
			array(
				'id' => '113',
				'ncount' => 1258,
			),
			114 =>
			array(
				'id' => '114',
				'ncount' => 1332,
			),
			115 =>
			array(
				'id' => '115',
				'ncount' => 1360,
			),
			116 =>
			array(
				'id' => '116',
				'ncount' => 1077,
			),
			117 =>
			array(
				'id' => '117',
				'ncount' => 631,
			),
			118 =>
			array(
				'id' => '118',
				'ncount' => 440,
			),
			119 =>
			array(
				'id' => '119',
				'ncount' => 282,
			),
			120 =>
			array(
				'id' => '120',
				'ncount' => 1932,
			),
			121 =>
			array(
				'id' => '121',
				'ncount' => 163,
			),
			122 =>
			array(
				'id' => '122',
				'ncount' => 579,
			),
			141 =>
			array(
				'id' => '141',
				'ncount' => 118,
			),
			142 =>
			array(
				'id' => '142',
				'ncount' => 160,
			),
			143 =>
			array(
				'id' => '143',
				'ncount' => 131,
			),
			144 =>
			array(
				'id' => '144',
				'ncount' => 127,
			),
			145 =>
			array(
				'id' => '145',
				'ncount' => 105,
			),
			146 =>
			array(
				'id' => '146',
				'ncount' => 3,
			),
			147 =>
			array(
				'id' => '147',
				'ncount' => 0,
			),
			150 =>
			array(
				'id' => '150',
				'ncount' => 120,
			),
		),
	),
);

function cmp_sort($a, $b)
{
	if ($a['ncount'] == $b['ncount']) {
		return 0;
	}

	return ($a['ncount'] > $b['ncount']) ? -1 : 1;
}
uasort($array, 'cmp_sort');

print_r($array);
  • Вопрос задан
  • 126 просмотров
Подписаться 2 Простой 3 комментария
Решения вопроса 1
@kosta6832 Автор вопроса
function cmp_sort($a, $b)
{
	if ($a['ncount'] == $b['ncount']) {
		return 0;
	}

	return ($a['ncount'] > $b['ncount']) ? -1 : 1;
}
function arrSort(array &$arr)
{
    uasort($arr, 'cmp_sort');
    foreach ($arr as $k => &$v) {
        if(isset($v['children'])){
            arrSort($v['children']);
        }
    }
}
arrSort($array);
print_r($array);
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы