How do PHP arrays sort by one of these values? Resolvent

Time:2019-12-11

There is an array as follows

 

I want to sort it by the size of its “sort” value, 3 – > 4 – > 7.

The solution is as follows:

1. First, introduce two methods of PHP

Method 1: array ou column() returns the value of a single column in the input array. PHP version 5.5 +.

parameter describe
array Necessary. Specifies the multidimensional array (recordset) to use.
column_key

Necessary. Column that needs to return a value.

It can be the integer index of the column of the index array, or the string key value of the column of the associated array.

This parameter can also be null. At this time, the entire array will be returned (it is very useful to reset the array key with the index_key parameter).

index_key Optional. The column used as the index / key to return the array.

Example 1:

         $cmf_settings = array(
             array (
                 "url" => "img/20191015/5da586020fa6b.jpg",
                 "alt" => "lunbo2",
                 "href" => "https://www.baidu.com/",
                 "sort" => "7"
             ),
             array(
                 "url" => "img/20191018/5da9783499dec.jpg",
                 "alt" => "X1300",
                 "href" => "https://www.baidu.com/fully-automatic-pallet-wrappin/x1300-180.html",
                 "sort" => "3"
             ),
             array(
                 "url" => "img/20191018/5da987c81f420.jpg",
                 "alt" => "S300",
                 "href" => "https://www.baidu.com/fully-automatic-pallet-wrappin/s300-170.html",
                 "sort" => "4"
             )
         );
 
         $cmf_arr = array_column($cmf_settings, 'sort');

Print $CMF? Arr to get the following results

 

Method 2: the array UU multisort() function returns a sort array. You can enter one or more arrays. The function sorts the first array, followed by other arrays. If two or more values are the same, it sorts the next array.

Array [multisort (‘array 1 ‘,’ sort [ASC (ascending), ‘array 2’, ‘array 3’, ‘array 4’, ‘array 5’, ‘array 6’);

Array 1: array to sort.

Sort? ASC: it is the sort method of array 1.

‘array 2’, ‘array 3’…: sort ‘array 2’, ‘array 3’, ‘array 4’, ‘array 5’, and ‘array 6’ according to the sorting method of array 1.

Ascending sorting of array 1: if the position of the second group of data [1] = > 3 becomes [0] = > 3, then the position of the second group of data [1] = > no matter what data becomes [0] = > no matter what data is.

parameter describe
array1 Necessary. Specifies the array.
sorting order Optional. Specify the order of arrangement. Possible values:

  • Sort? ASC – default. In ascending order (A-Z).
  • Sort? Desc – in descending order (z-a).
sorting type Optional. Specifies the sort type. Possible values:

  • Sort? Default. Arrange each item in the normal order (standard ascii, do not change the type).
  • Sort menu – treat each item as a number.
  • Sort_string – treat each item as a string.
  • Sort? Local? String – treats each item as a string, based on the current locale (which can be changed by setlocale()).
  • Sort_natural – treat each item as a string, using a natural sort similar to natsort().
  • Sort? Flag? Case – you can sort strings in combination (bitwise OR) with sort? String or sort? Natural, case insensitive.
array2 Optional. Specifies the array.
array3 Optional. Specifies the array.

 

Example two:

         $cmf_settings = array(
             array (
                 "url" => "img/20191015/5da586020fa6b.jpg",
                 "alt" => "lunbo2",
                 "href" => "https://www.baidu.com/",
                 "sort" => "7"
             ),
             array(
                 "url" => "img/20191018/5da9783499dec.jpg",
                 "alt" => "X1300",
                 "href" => "https://www.baidu.com/fully-automatic-pallet-wrappin/x1300-180.html",
                 "sort" => "3"
             ),
             array(
                 "url" => "img/20191018/5da987c81f420.jpg",
                 "alt" => "S300",
                 "href" => "https://www.baidu.com/fully-automatic-pallet-wrappin/s300-170.html",
                 "sort" => "4"
             )
         );
         $cmf_arr = array_column($cmf_settings, 'sort');
         ($cmf_arr, SORT_ASC, $cmf_settings);

Print $CMF? Settings to get the following results to meet the needs.

Summary: extract the key values to be sorted by using array column(), and sort the array by using array multi sort(). It’s a bit confusing to understand, but it will be of great use in the future.

Please point out any mistakes, thank you.