There follows an array
I would be a sort, sorted by size value which is "sort", the 3-> 4-> 7.
Solutions are as follows:
1, first introduced two methods of php
Method a: array_column () returns an array of a single input value column. php version 5.5+.
parameter | description |
---|---|
array | essential. Multidimensional array (record set) provisions to be used. |
column_key | essential. We need to return column values. It may be an integer index into the array column index, or row associated string key array. This parameter may be NULL, the entire array will be returned (reset time parameter fit index_key key array, it is useful). |
index_key | Optional. It returns an array of columns as the index / key. |
Example 1:
1 $cmf_settings = array( 2 array ( 3 "url" => "img/20191015/5da586020fa6b.jpg", 4 "alt" => "lunbo2", 5 "href" => "https://www.baidu.com/", 6 "sort" => "7" 7 ), 8 array( 9 "url" => "img/20191018/5da9783499dec.jpg", 10 "alt" => "X1300", 11 "href" => "https://www.baidu.com/fully-automatic-pallet-wrappin/x1300-180.html", 12 "sort" => "3" 13 ), 14 array( 15 "url" => "img/20191018/5da987c81f420.jpg", 16 "alt" => "S300", 17 "href" => "https://www.baidu.com/fully-automatic-pallet-wrappin/s300-170.html", 18 "sort" => "4" 19 ) 20 ); 21 22 $cmf_arr = array_column($cmf_settings, 'sort');
Print $ cmf_arr following results
Method two: array_multisort () function returns a sorted array. You can enter one or more arrays. Function to sort the array of first, followed by the other array, if the same two or more values, it sorts the next array.
array_multisort ( 'array 1', 'SORT_ASC (ascending)', 'array 2', '3 array', 'array 4', '5 array', 'array 6');
Array 1: sorted array.
SORT_ASC (ascending): Sort the array is a 1.
'Array 2', '3 array' ......: The ordering of the array 1 'array 2', '3 array', 'array 4', '5 array', 'array 6' sort.
1 array in ascending order: a second set of data [1] => "3" position becomes [0] => "3", then the 'array 2', '3 array', 'array 4', '5 array' the second set of data 'array 6' [1] => "no matter what the data is" position becomes [0] => "no matter what the data is."
参数 | 描述 |
---|---|
array1 | 必需。规定数组。 |
sorting order | 可选。规定排列顺序。可能的值:
|
sorting type | 可选。规定排序类型。可能的值:
|
array2 | 可选。规定数组。 |
array3 | 可选。规定数组。 |
Example 2:
1 $cmf_settings = array( 2 array ( 3 "url" => "img/20191015/5da586020fa6b.jpg", 4 "alt" => "lunbo2", 5 "href" => "https://www.baidu.com/", 6 "sort" => "7" 7 ), 8 array( 9 "url" => "img/20191018/5da9783499dec.jpg", 10 "alt" => "X1300", 11 "href" => "https://www.baidu.com/fully-automatic-pallet-wrappin/x1300-180.html", 12 "sort" => "3" 13 ), 14 array( 15 "url" => "img/20191018/5da987c81f420.jpg", 16 "alt" => "S300", 17 "href" => "https://www.baidu.com/fully-automatic-pallet-wrappin/s300-170.html", 18 "sort" => "4" 19 ) 20 ); 22 $cmf_arr = array_column($cmf_settings, 'sort'); 24 array_multisort($cmf_arr, SORT_ASC, $cmf_settings);
Print $ cmf_settings results are as follows, to meet the demand.
Summary: use array_column () to extract the key to be sorted out, using array_multisort () to sort the array. To understand a little about, but the future will be an enormous usefulness.
Please point out any errors, thank you.