简体   繁体   English

按特定键值对多维数组进行排序

[英]Sort multidimensional array by specific key value

I'm working on algorithm to display my events on the website.我正在研究算法以在网站上显示我的事件。 I want to sort my multidimensional array by specific key value.我想按特定键值对我的多维数组进行排序。

My array:我的数组:

    ["2022-02-28"]=>
  array(1) {
    [0]=>
    array(3) {
      ["post_id"]=>
      string(4) "3656"
      ["time"]=>
      string(5) "16:05"
      ["priority"]=>
      string(1) "0"
    }
  }
  ["2022-03-01"]=>
  array(2) {
    [2]=>
    array(3) {
      ["post_id"]=>
      string(4) "3656"
      ["time"]=>
      string(5) "16:05"
      ["priority"]=>
      string(1) "0"
    }
    [3]=>
    array(3) {
      ["post_id"]=>
      string(4) "3784"
      ["time"]=>
      string(5) "13:00"
      ["priority"]=>
      string(1) "0"
    }
  }
  ["2022-03-03"]=>
  array(1) {
    [5]=>
    array(3) {
      ["post_id"]=>
      string(4) "3663"
      ["time"]=>
      string(5) "13:06"
      ["priority"]=>
      string(1) "1"
    }
  }
}

I want to sort the array by "time" key value.我想按“时间”键值对数组进行排序。 So for example at this index:因此,例如在此索引处:

    ["2022-03-01"]=>
  array(2) {
    [2]=>
    array(3) {
      ["post_id"]=>
      string(4) "3656"
      ["time"]=>
      string(5) "16:05"
      ["priority"]=>
      string(1) "0"
    }
    [3]=>
    array(3) {
      ["post_id"]=>
      string(4) "3784"
      ["time"]=>
      string(5) "13:00"
      ["priority"]=>
      string(1) "0"
    }
  }

I want first 13:00 to appear then 16:05.我希望先出现 13:00,然后出现在 16:05。 Thank you for your help in advance: :)提前谢谢你的帮助: :)

Use usort for define custom sort.使用usort定义自定义排序。

function time_sort(array $arr){
  usort($arr, function($a, $b){
    return strcmp($a['time'], $b['time']);
  });
}

Try with this:试试这个:

<?php 


$arr = array();

$arr["2022-02-28"] = [
    array("post_id"=>"3656", "time"=>"16:05", "priority"=>"0"),
    array("post_id"=>"4856", "time"=>"13:05", "priority"=>"3")];
$arr["2022-03-01"] = [
    array("post_id"=>"3656", "time"=>"16:05", "priority"=>"0"),
    array("post_id"=>"3636", "time"=>"13:05", "priority"=>"1")
    ];



foreach($arr as $key => $value){
    
   usort($value, function($a,$b){
       return strtotime($a["time"])>strtotime($b["time"]);
    });
         
    $arr[$key] = $value;  

}

echo "<pre>";
var_dump($arr);
echo "</pre>";

Output: Output:

array(2) {
["2022-02-28"]=>
array(2) {
  [0]=>
  array(3) {
    ["post_id"]=>
    string(4) "4856"
    ["time"]=>
    string(5) "13:05"
    ["priority"]=>
    string(1) "3"
  }
  [1]=>
  array(3) {
    ["post_id"]=>
    string(4) "3656"
    ["time"]=>
    string(5) "16:05"
    ["priority"]=>
    string(1) "0"
  }
}
["2022-03-01"]=>
array(2) {
  [0]=>
  array(3) {
    ["post_id"]=>
    string(4) "3636"
    ["time"]=>
    string(5) "13:05"
    ["priority"]=>
    string(1) "1"
  }
  [1]=>
  array(3) {
    ["post_id"]=>
    string(4) "3656"
    ["time"]=>
    string(5) "16:05"
    ["priority"]=>
    string(1) "0"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM