ph two-dimensional array sorting puzzles, requesting help with detailed requirements

Source: Internet
Author: User
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.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.