PHP array function array_multisort() usage example analysis, arraymultisort_PHP tutorial

WBOY
Release: 2016-07-12 08:55:27
Original
758 people have browsed it

PHP array function array_multisort() usage example analysis, arraymultisort

This article analyzes the PHP array function array_multisort() usage example. Share it with everyone for your reference, the details are as follows:

Sometimes we need to sort the value of a key in a two-dimensional array. This issue is discussed here. We can use the array_multisort() function. The array_multisort() function sorts multiple arrays or multidimensional arrays.

The array in the parameter is treated as a table column and sorted by row - this is similar to the functionality of SQL's ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array compare to be the same, they will be sorted according to the size of the corresponding value in the next input array, and so on.

The first parameter is an array, and each subsequent parameter may be an array or one of the following sort order flags (the sort flag is used to change the default sort order):

SORT_ASC - Default, sort in ascending order. (A-Z)
SORT_DESC - Sort in descending order. (Z-A)

The type of sort can then be specified:

SORT_REGULAR - Default. Arrange each item in regular order.
SORT_NUMERIC - Sort each item in numerical order.
SORT_STRING - Sort each item in alphabetical order.

Syntax: array_multisort(array1,sorting order,sorting type,array2,array3...)

array1: required. Specifies the input array.
sorting order: optional. Specify the order of sorting. Possible values ​​are SORT_ASC and SORT_DESC.
sorting type: optional. Specifies the sorting type. Possible values ​​are SORT_REGULAR, SORT_NUMERIC, and SORT_STRING.
array2: optional. Specifies the input array.
array3: optional. Specifies the input array.

String key names will be preserved, but numeric keys will be re-indexed, starting at 0 and increasing by 1. The sort order and sort type can be set after each array. If not set, each array parameter will use its default value.

Here is an example:

<&#63;php
 $arr = '';
 echo '二维数组如下:'.'<br / >';
 for($i=0; $i<=5; $i++)
 {
 $arr[$i]['val'] = mt_rand(1, 100);
 $arr[$i]['num'] = mt_rand(1, 100);
 }
 echo '<pre class="brush:php;toolbar:false">';
 print_r($arr);
 echo '
'; echo '从二维数组中抽出键为val,单独成另一个数组:'.'
'; foreach ($arr as $key => $row) { $vals[$key] = $row['val']; $nums[$key] = $row['num']; } echo '
';
 print_r($vals);
 echo '
'; echo '对其进行排序:'.'
'; array_multisort($vals, SORT_ASC, $arr); echo '
';
 print_r($vals);
 echo '
'; ?>
Copy after login

Run result:

二维数组如下:
Array
(
  [0] => Array
    (
      [val] => 46
      [num] => 49
    )
  [1] => Array
    (
      [val] => 8
      [num] => 24
    )
  [2] => Array
    (
      [val] => 37
      [num] => 3
    )
  [3] => Array
    (
      [val] => 32
      [num] => 35
    )
  [4] => Array
    (
      [val] => 19
      [num] => 38
    )
  [5] => Array
    (
      [val] => 30
      [num] => 37
    )
)

 
从二维数组中抽出键为val,单独成另一个数组:

Array
(
  [0] => 46
  [1] => 8
  [2] => 37
  [3] => 32
  [4] => 19
  [5] => 30
)

对其进行排序:

Array
(
  [0] => 8
  [1] => 19
  [2] => 30
  [3] => 32
  [4] => 37
  [5] => 46
)

Copy after login

In this way we will get a two-dimensional array sorted by val in ascending order.

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "Introduction to PHP basic syntax", "php operation office documentation" Summary of skills (including word, excel, access, ppt)", "Summary of php date and time usage", "Introduction to php object-oriented programming tutorial", "Summary of php string (string) usage", "Introduction to php mysql database operation" Tutorial" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • Usage of shuffle array value random sorting function in PHP
  • Instance of implementing PHP's shuffled array function shuffle in JavaScript
  • php array function sequence shuffle() and array_rand() Introduction to the use of random functions
  • Summary of common array function usage in PHP
  • Summary of php array function array_key_exists()
  • In PHP Use the array function to create a new array
  • php Use the array_slice function to get a random array or the first few pieces of data
  • php Use the array_search function to implement array search
  • PHP array and explode function Summary of examples
  • PHP uses the in_array function to check whether a certain value exists in the array
  • Summary of PHP array related functions
  • PHP uses the gettimeofday function to return the current time and store it in an associative array
  • Usage of PHP function end() to access the last element of the array
  • Analysis of the method of PHP function shuffle() to get several random elements of the array

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117039.htmlTechArticlePHP array function array_multisort() usage example analysis, arraymultisort This article analyzes the PHP array function array_multisort() usage example. Share it with everyone for your reference, the details are as follows: Yes...
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