当前位置:网站首页>PHP 2D / multidimensional arrays are sorted in ascending and descending order according to the specified key values

PHP 2D / multidimensional arrays are sorted in ascending and descending order according to the specified key values

2022-06-26 05:10:00 You are sexy!

If you don't want to talk much, please go to the picture above :

Obviously this is a multidimensional array , According to the requirements of the front end , He asked me to $data['children'] Data inside , according to id To pass it on to him in ascending order .

Then this is simple .( You can't say you won't do it in front of the front end )

Let's first look at the data structure of the array
 

array(1) {
  [0]=>
  array(5) {
    ["id"]=>
    int(1)
    ["pid"]=>
    int(0)
    ["cate_name"]=>
    string(6) " menu "
    ["pic"]=>
    string(0) ""
    ["children"]=>
    array(6) {
      [0]=>
      array(3) {
        ["id"]=>
        int(8)
        ["cate_name"]=>
        string(9) " Local specialty "
        ["pic"]=>
        string(0) ""
      }
      [1]=>
      array(3) {
        ["id"]=>
        int(7)
        ["cate_name"]=>
        string(12) " Drinks "
        ["pic"]=>
        string(0) ""
      }
      [2]=>
      array(3) {
        ["id"]=>
        int(6)
        ["cate_name"]=>
        string(6) " Staple food "
        ["pic"]=>
        string(0) ""
      }
      [3]=>
      array(3) {
        ["id"]=>
        int(5)
        ["cate_name"]=>
        string(6) " Stir fry "
        ["pic"]=>
        string(0) ""
      }
      [4]=>
      array(3) {
        ["id"]=>
        int(4)
        ["cate_name"]=>
        string(6) " Cold Dishes "
        ["pic"]=>
        string(0) ""
      }
      [5]=>
      array(3) {
        ["id"]=>
        int(2)
        ["cate_name"]=>
        string(12) " Key features "
        ["pic"]=>
        string(0) ""
      }
    }
  }
}

Don't talk much , Look at how I put $data['children'] How the following data is in ascending order Sort of :


/************* This is the query . Can omit not to see ****************************/

// The first step is to query the data , I use it Tp6.0 frame 
$cateogry = StoreCategory::with('children')

            ->where('is_show',1)

            ->order('sort desc,id asc')

            ->where('pid',0)

            ->hidden(['add_time','is_show','sort','children.sort','children.add_time','children.pid','children.is_show'])

            ->select()

            ->toArray();

/************* This is the query . Can omit not to see ****************************/


// The second step   Take out the array to be arranged 
$a = $cateogry[0]['children'];

// Step 3: take it out again   Take out the key of the reference to be sorted       
$cmf_arr = array_column($a, 'id');
        
// Fourth parts   Sort in ascending order ( In this step, do not use variables to receive ,array_multisort() The return is a Boolean value ) 
array_multisort($cmf_arr,SORT_ASC, $a);
   
// Step five   Reassign , Replace array     
$cateogry[0]['children'] = $a;

// Step six , Back to the front end 
return app('json')->success($cateogry);

Let's look at the results again :

 

Take another look at the data returned to the front end :

Okay , This has met the requirements of the front end API Asked for .

 

 

原网站

版权声明
本文为[You are sexy!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260504427524.html