php how to sort an array based on a value which? Solution

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 可选。规定排列顺序。可能的值:
  • SORT_ASC - 默认。按升序排列 (A-Z)。
  • SORT_DESC - 按降序排列 (Z-A)。
sorting type 可选。规定排序类型。可能的值:
  • SORT_REGULAR - 默认。把每一项按常规顺序排列(Standard ASCII,不改变类型)。
  • SORT_NUMERIC - 把每一项作为数字来处理。
  • SORT_STRING - 把每一项作为字符串来处理。
  • SORT_LOCALE_STRING - 把每一项作为字符串来处理,基于当前区域设置(可通过 setlocale() 进行更改)。
  • SORT_NATURAL - 把每一项作为字符串来处理,使用类似 natsort() 的自然排序。
  • SORT_FLAG_CASE - 可以结合(按位或)SORT_STRING 或 SORT_NATURAL 对字符串进行排序,不区分大小写。
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.

 

Guess you like

Origin www.cnblogs.com/lzijiangg/p/11731298.html