[Solution] PHP multi-dimensional array sorting

Source: Internet
Author: User
[Problem solving] PHP multi-dimensional array sorting recently encountered a problem involving multi-dimensional array sorting. Each array of this series has a key PID, which requires that the array be rearranged according to the PID. Just sort the first layer of array. Array_multisort () is used, but an error is returned. As follows:
Array ([0] => Array ([pid] => 0 [title] => homepage [path] => index [current] => 0 [link] =>/index. php) [1] => Array ([tid] => 3 [pid] => 8 [mid] => 8 [topid] => 0 [upid] => 0 [exmid] => 0 [linkid] => 2 [gotoid] => 17 [lng] => cn [larray] => Array ([0] => Array ([tid] => 17 [pid] => 50 [mid] => 8 [topid] => 3 [upid] => 3) [1] => Array ([tid] => 18 [pid] => 50 [mid] => 8 [topid] => 3 [upid] => 3) [2] => Array ([tid] => 19 [pid] => 50 [mid] => 8 [topid] => 3 [upid] => 3 ))) [2] => Array ([tid] => 5 [pid] => 2 [mid] => 3 [topid] => 0 [upid] => 0 [exmid] => 0 [larray] => Array ([0] => Array ([tid] => 11 [pid] => 50 [mid] => 3 [topid] => 5 [upid] => 5) [1] => Array ([tid] => 12 [pid] => 50 [mid] => 3 [topid] => 5 [upid] => 5) [2] => Array ([tid] => 22 [pid] => 50 [mid] => 8 [topid] => 5 [upid] => 5 ))) [3] => Array ([tid] => 50 [pid] => 4 [mid] => 8 [topid] => 0 [upid] => 0 [larray] => ))


The final result is as follows: the PID is rearranged from small to large.
Array ([0] => Array ([pid] => 0 [title] => homepage [path] => index [current] => 0 [link] =>/index. php) [1] => Array ([tid] => 5 [pid] => 2 [mid] => 3 [topid] => 0 [upid] => 0 [exmid] => 0 [larray] => Array ([0] => Array ([tid] => 11 [pid] => 50 [mid] => 3 [topid] => 5 [upid] => 5) [1] => Array ([tid] => 12 [pid] => 50 [mid] => 3 [topid] => 5 [upid] => 5) [2] => Array ([tid] => 22 [pid] => 50 [mid] => 8 [topid] => 5 [upid] => 5 ))) [2] => Array ([tid] => 50 [pid] => 4 [mid] => 8 [topid] => 0 [upid] => 0 [larray] =>) [3] => Array ([tid] => 3 [pid] => 8 [mid] => 8 [topid] => 0 [upid] => 0 [exmid] => 0 [linkid] => 2 [gotoid] => 17 [lng] => cn [larray] => Array ([0] => Array ([tid] => 17 [pid] => 50 [mid] => 8 [topid] => 3 [upid] => 3) [1] => Array ([tid] => 18 [pid] => 50 [mid] => 8 [topid] => 3 [upid] => 3) [2] => Array ([tid] => 19 [pid] => 50 [mid] => 8 [topid] => 3 [upid] => 3 ))))


Reply to discussion (solution)

We recommend that you paste the array after var_export (); for testing.

And other experts, I will only choose to sort

If the original array is set to $ ar

array_multisort(array_map(function($item){ return $item['pid']; }, $ar), $ar);

We recommend that you paste the array after var_export (); for testing.
This is printed by print_r.

We recommend that you paste the array after var_export (); for testing.

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.