"Solving Problems" PHP multidimensional array sorting problem

Source: Internet
Author: User
Recently touched PHP, just encountered a problem, involving the problem of multi-dimensional array sorting. The array has a key PID, which requires the array to be rearranged again by PID. Just sort the first-level array. Array_multisort () was used, but an error occurred. As follows:
Array ([0] = = Array ([PID] = 0 [Title] = home [path] = Index            [Current] = 0 [link] =/index.php) [1] = = Array ([tid] = 3 [PID] = 8 [Mid] = 8 [Topid] = 0 [upid] + 0 [Exmid]                 + 0 [LinkId] + 2 [gotoid] = [LNG] = cn [Larray] = = Array                            ([0] = = Array ([tid] = 17 [PID] [Mid] = 8 [topid] = =                        3 [Upid] = 3) [1] = = Array ([Tid] = [PID] = [mid               ] = 8             [Topid] = 3 [upid] + 3) [2] =&G T                            Array ([tid] = + [PID] = 50                        [Mid] = 8 [Topid] = 3 [Upid] + 3 ))) [2] = = Array ([tid] = 5 [PID] =&gt ; 2 [Mid] = 3 [Topid] = 0 [Upid] = 0 [Exmid] + 0 [lar Ray] = Array ([0] = = Array ([                            TID] = [PID] = [Mid] = 3                      [Topid] = 5 [Upid] = 5) [1] = = Array  ([Tid] = [PID] = [Mid] =                    > 3 [Topid] = 5 [upid] 5)                            [2] = = Array ([tid] = 22                            [PID] = [Mid] = 8 [Topid] + 5  [Upid] + 5)) [3] = = Array ([tid] = = [PID] = 4 [Mid] = 8 [Topid] = 0 [Upid] = 0 [lar Ray] =))


The end result is as follows: The PID was rearranged from small to large.
Array ([0] = = Array ([PID] = 0 [Title] = home [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] = [PID] = [Mid] = 3                    [Topid] + 5 [upid] = 5) [1] = = Array ([tid] = [PID] = > [Mid] = 3 [Topid] = 5 [Up             ID] = 5           ) [2] = = Array ([tid] = 22                            [PID] = [Mid] = 8 [Topid] + 5            [Upid] + 5))) [2] = = Array (  [Tid] = [PID] = 4 [Mid] = 8 [topid] + 0 [upid] = =            0 [Larray] + =) [3] = = Array ([tid] = 3 [PID] + 8 [Mid] = 8 [Topid] = 0 [Upid] = 0 [Exmid] + 0 [LinkId] =& Gt                    2 [gotoid] = [LNG] = cn [Larray] = = Array ( [0] = = Array ([tid] = + [PID] =&G T               50             [Mid] = 8 [Topid] = 3 [Upid] + 3                            ) [1] = = Array ([tid] = 18                            [PID] = [Mid] = 8 [Topid] + 3                        [Upid] = 3) [2] = = Array ([Tid] = [PID] + [Mid] =                > 8 [Topid] = 3 [upid] 3) )        )   )


Reply to discussion (solution)

It is recommended to post an array of Var_export () to test

And so the master, I will only choose the sort

Set the original array to $ar, there is

Array_multisort (Array_map (function ($item) {return $item [' pid '];}, $ar), $ar);

It is recommended to post an array of Var_export () to test
This is what print_r printed out.

It is recommended to post an array of Var_export () to test

  • 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.