Please help me sort php 2D arrays again

Source: Internet
Author: User
Please help me to re-sort the php 2D Array ('id' => 3, 'username' => 'Alex ', 'parentname' => 'John'
, 'Proname' => 'tttttt' 'num' => 1, 'userid' => 6, 'parentid' => 8,
'Cpid' => 0, 'mobile' => 15533336666, 'parentmobile' => 156477765465 ));
How can I re-sort the last two places in the 2nd and 4th locations?
Array ('id' => 3, 'username' => 'Alex ', 'mobile' => 15533336666
, 'Parentname' => 'John', 'parentmobile '=> 156477765465, 'proname' => 'tttttt' 'num' => 1,
'Userid' => 6, 'parentid' => 8, 'cpid' => 0 );


Reply to discussion (solution)

What sort do you consider?
Isn't it just about tuning the location? Why?

Finally, I want to use excel for output. if the sorting is not like this, the format will be messy.

Is there a way to sort 100 records from the database?

What sort do you consider?
Isn't it just about tuning the location? Why?



What sort do you consider?
Isn't it just about tuning the location? Why?




Is there a way to adjust the location? Thank you.

Is there a way to sort 100 records from the database?


Slave Database? Can you directly change the order when querying?




Is there a way to sort 100 records from the database?


Slave Database? Can you directly change the order when querying?



The last two fields are the result of indirect query from another table and then appended with an array, which leads to the current location problem.

1. do not simply select *
Instead, select id, username, mobile... is listed one by one, and the order you requested is not there.
2. php code can also be used.

$a = Array ( Array ('id' => 3, 'username' => 'alex ','parentname'=> 'john'         ,'proname' => 'tttt', 'num'=> 1 ,  'userid' =>6 ,'parentid'=>8 ,   'cpid'=>  0 ,'mobile'=> 15533336666 ,'parentmobile' => 156477765465) );$k = Array ('id', 'username', 'mobile', 'parentname', 'parentmobile', 'proname', 'num', 'userid', 'parentid', 'cpid');foreach($a as $i=>$v) {  extract($v);  $a[$i] = compact($k);}print_r($a);
Array(    [0] => Array        (            [id] => 3            [username] => alex             [mobile] => 15533336666            [parentname] => john            [parentmobile] => 156477765465            [proname] => tttt            [num] => 1            [userid] => 6            [parentid] => 8            [cpid] => 0        ))

Your key => value
How to arrange does not affect the use.

But you can write it like this.

 3,'username'=>'alex','parentname'=>'john','proname'=>'tttt','num'=> 1 ,  'userid' =>6 ,'parentid'=>8 ,'cpid'=>  0 ,'mobile'=> 15533336666 ,'parentmobile' => 156477765465),    array('id'=>3,'username'=>'alex','parentname'=>'john','proname'=>'tttt','num'=> 1 ,  'userid' =>6 ,'parentid'=>8 ,'cpid'=>  0 ,'mobile'=> 15533336666 ,'parentmobile' => 156477765465),    array('id'=>3,'username'=>'alex','parentname'=>'john','proname'=>'tttt','num'=> 1 ,  'userid' =>6 ,'parentid'=>8 ,'cpid'=>  0 ,'mobile'=> 15533336666 ,'parentmobile' => 156477765465),);$result = array();$fields = array('id','username','mobile','parentname','parentmobile','proname','num','userid','parentid','cpid');foreach($arr as $k=>$v){    $tmp = array();    foreach($fields as $f){        $tmp[$f] = $v[$f];    }    $result[] = $tmp;}print_r($result);

Thank you for your advice.

If the problem is solved, please close the post.

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.