Ph two-dimensional array sorting problem, with detailed requirements

Source: Internet
Author: User
The two-dimensional array sorting problem of ph. for help, the two-dimensional array with detailed requirements for initial status is like this $ a & nbsp; array (& nbsp; array (name & gt; a, num & gt; 1, parentNum & gt; 0), & nbsp; array (name & gt; B, num & gt; 2. parent ph: two-dimensional array sorting problem, request help, with detailed requirements included
The two-dimensional array of the initial state is like this.
$a = array(
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'),
)

Sort by name by default, but change now
It is still sorted by the name field, but also by the parentNum field. parentNum indicates its parent category, so the final 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 can I implement it using code? I tried for a long time. why not?
------ Solution ----------------------
$a = array(
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);
print_r($a);
Array
(
[0] => Array
(
[name] => a
[num] => 1
[parentNum] => 0
)

[1] => Array
(
[name] => d
[num] => 4
[parentNum] => 1
)

[2] => Array
(
[name] => b
[num] => 2
[parentNum] => 0
)

[3] => Array
(
[name] => e
[num] => 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
)

)

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.