Home >Backend Development >PHP Tutorial >PHP code to sort multidimensional arrays by a certain value

PHP code to sort multidimensional arrays by a certain value

WBOY
WBOYOriginal
2016-07-25 09:07:38846browse
  1. $arr = array(
  2. "0" => array(line_num => "10", occur_time => "2007-11-19 10:25:04+08"),
  3.    "1" => array(line_num => "9", occur_time => "2007-11-19 10:25:04+08"),
  4.  "2" => array(line_num => "25", occur_time => "2007-11-19 10:25:04+08")
  5. );
  6. print_r($arr);
  7. echo "
    ";
  8. echo "
    ";
  9. echo "
    ";
  10. uasort( $arr,create_function('$a, $b', 'return $a['line_num'] < $b['line_num'];') );
  11. print_r($arr);
  12. echo "
    ";
  13. echo "
    ";
  14. echo "
    ";
  15. foreach($arr as $key => $value) {
  16. $ar[] = $value;
  17. }
  18. print_r($ar);
  19. ?>
复制代码

输出结果: Array ( [0] => Array ( [line_num] => 10 [occur_time] => 2007-11-19 10:25:04+08 ) [1] => Array ( [line_num] => 9 [occur_time] => 2007-11-19 10:25:04+08 ) [2] => Array ( [line_num] => 25 [occur_time] => 2007-11-19 10:25:04+08 ) )


Array ( [2] => Array ( [line_num] => 25 [occur_time] => 2007-11-19 10:25:04+08 ) [0] => Array ( [line_num] => 10 [occur_time] => 2007-11-19 10:25:04+08 ) [1] => Array ( [line_num] => 9 [occur_time] => 2007-11-19 10:25:04+08 ) )


Array ( [0] => Array ( [line_num] => 25 [occur_time] => 2007-11-19 10:25:04+08 ) [1] => Array ( [line_num] => 10 [occur_time] => 2007-11-19 10:25:04+08 ) [2] => Array ( [line_num] => 9 [occur_time] => 2007-11-19 10:25:04+08 ) )

>>> 更多内容,请查看 php数组排序方法大全



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn