Home > Backend Development > PHP Tutorial > Detailed explanation of the use cases of the two-dimensional array sorting function array_orderby

Detailed explanation of the use cases of the two-dimensional array sorting function array_orderby

php中世界最好的语言
Release: 2023-03-26 11:56:01
Original
1370 people have browsed it

This time I will bring you two-dimensional Array sortingDetailed explanation of the use cases of the function array_orderby. What are the precautions for using the two-dimensional array sorting function array_orderby. The following is a practical case. Let's take a look.

<?php
/**
I came up with an easy way to sort database-style results. This does what example 3 does, except it takes care of creating those intermediate arrays for you before passing control on to array_multisort(). 
*/
function array_orderby()
{
  $args = func_get_args();
  $data = array_shift($args);
  foreach ($args as $n => $field) {
    if (is_string($field)) {
      $tmp = array();
      foreach ($data as $key => $row)
        $tmp[$key] = $row[$field];
      $args[$n] = $tmp;
      }
  }
  $args[] = &$data;
  call_user_func_array(&#39;array_multisort&#39;, $args);
  return array_pop($args);
}
/*
The sorted array is now in the return value of the function instead of being passed by reference.
*/
$data[] = array(&#39;volume&#39; => 67, &#39;edition&#39; => 2);
$data[] = array(&#39;volume&#39; => 86, &#39;edition&#39; => 1);
$data[] = array(&#39;volume&#39; => 85, &#39;edition&#39; => 6);
$data[] = array(&#39;volume&#39; => 98, &#39;edition&#39; => 2);
$data[] = array(&#39;volume&#39; => 86, &#39;edition&#39; => 6);
$data[] = array(&#39;volume&#39; => 67, &#39;edition&#39; => 7);
// Pass the array, followed by the column names and sort flags
$sorted = array_orderby($data, &#39;volume&#39;, SORT_DESC, &#39;edition&#39;, SORT_ASC);
print_r($sorted)
?>
Copy after login

Run results:

Array
(
  [0] => Array
    (
      [volume] => 98
      [edition] => 2
    )
  [1] => Array
    (
      [volume] => 86
      [edition] => 1
    )
  [2] => Array
    (
      [volume] => 86
      [edition] => 6
    )
  [3] => Array
    (
      [volume] => 85
      [edition] => 6
    )
  [4] => Array
    (
      [volume] => 67
      [edition] => 2
    )
  [5] => Array
    (
      [volume] => 67
      [edition] => 7
    )
)
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

ThinkPHP database connection operation case analysis

ThinkPHP framework PDO connection database steps detailed explanation

The above is the detailed content of Detailed explanation of the use cases of the two-dimensional array sorting function array_orderby. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template