The initial state of the two-dimensional array is this
$a = array ( ' name ' = ' a ', ' num ' = ' 1 ', ' parentnum ' = ' 0 '), Array (' name ' = = ' B ', ' num ' = ' 2 ', ' Parentnum ' = ' 0 '), Array (' name ' = = ' c ', ' num ' = ' 3 ', ' parentnum ' = ' 0 '), Array (' name ' = ' = ' d ', ' Num ' = ' 4 ', ' parentnum ' = ' 1 '), Array (' name ' = = ' e ', ' num ' = ' 5 ', ' parentnum ' = ' 2 '), Array (' Name ' = = ' f ', ' num ' = ' 6 ', ' parentnum ' + ' 0 '), Array (' name ' = = ' g ', ' num ' = ' 7 ', ' parentnum ' = ' 0 '), )
The default is sorted by the Name field, but now you need to change
Still sort by the name field, but also to sort by the Parentnum field, Parentnum is the parent category for it, so the end result should be
Array (' name ' = = ' a ', ' num ' = ' 1 ', ' parentnum ' = ' 0 '), Array (' name ' = = ' d ', ' num ' = ' 4 ', ' parentnum ' = > ' 1 '), Array (' name ' = = ' B ', ' num ' = ' 2 ', ' parentnum ' = ' 0 '), Array (' name ' = = ' e ', ' num ' = ' 5 ', ' Parentnum ' = ' 2 '), Array (' name ' = = ' c ', ' num ' = ' 3 ', ' parentnum ' = ' 0 '), Array (' name ' = ' ' f ', ' num ' = ' 6 ', ' parentnum ' = ' 0 '), Array (' name ' = = ' g ', ' num ' = ' 7 ', ' parentnum ' = ' 0 '),
How to use code to achieve it? I've been trying for quite a while.
Reply to discussion (solution)
$a = Array (' name ' = = ' a ', ' num ' = ' 1 ', ' parentnum ' = ' 0 '), Array (' name ' = > ' b ', ' num ' = ' 2 ', ' parentnum ' = ' 0 '), Array (' name ' = = ' c ', ' num ' = ' 3 ', ' parentnum ' = ' 0 '), Array (' Name ' + ' d ', ' num ' = ' 4 ', ' parentnum ' = ' 1 '), Array (' name ' = ' e ', ' num ' = ' 5 ', ' parentnum ' = ' 2 '), array (' name ' = ' f ', ' num ' = ' 6 ', ' parentnum ' = ' 0 '), Array (' name ' = ' = ' g ', ' num ' = ' 7 ', ' parentnum ' = ' 0 '),); foreach ($a as $r) {$p [] = $r [' Parentnum ']? $r [' Parentnum ']: $r [' num '];} Array_multisort ($p, $a);p rint_r ($a);
Array ( [0] = = Array ( [name] + a [num] = 1 [Parentnum] = 0 ) [1] = array< c7/> ( [name] = + D [num] = 4 [parentnum] + 1 ) [2] = = = Array ( [Name] = > b [num] = 2 [Parentnum] + 0 ) [3] = = Array ( [name] + e [num] =& Gt 5 [Parentnum] + 2 ) [4] = = Array ( [Name] = + c [num] = 3 [Parentnum] = > 0 ) [5] = = Array ([name] = f [num] = 6 [Parentnum] = 0 ) [ 6] = = Array ( [name] = g [num] = 7 [Parentnum] + 0 ))
The situation is a bit idealistic, but the actual is not right, or thank you
That's, but I can't do anything out of it.
Actually, this is not the sort of thing you can solve.
There are many posts in the essence area, you should take a serious look
If sorting can solve the query problem of adjacency list, does it require so many people to study him?
That's, but I can't do anything out of it.
Actually, this is not the sort of thing you can solve.
There are many posts in the essence area, you should take a serious look
If sorting can solve the query problem of adjacency list, does it require so many people to study him?
I've worked it out, I need to do it by hand, or thank you.