当前位置:网站首页>PHP 2D array sorted by a field

PHP 2D array sorted by a field

2022-08-09 23:14:00 Full stack programmer webmaster

Hello everyone, meet again, I'm your friend Quanstack Jun.

Data: data[] = array('customer_name' => 'Xiao Li', 'money' => 12, 'distance' => 2, 'address' => 'Chang'an Avenue C Square'); data[] = array('customer_name' => 'Zhao Xiaoya', 'money' => 89, 'distance' => 6, 'address' => 'Jiefang Road Henderson Building Block A'); data[] = array('customer_name' => 'Li Liangliang', 'money' => 45, 'distance' => 26, 'address' => 'Alley 198, Tianshan West Road');

Method 1:

function arraySort($array,$keys,$sort='asc') {$newArr = $valArr = array();foreach ($array as $key=>$value) {$valArr[$key] = $value[$keys];}($sort == 'asc') ? asort($valArr) : arsort($valArr);reset($valArr);foreach($valArr as $key=>$value) {$newArr[$key] = $array[$key];}return $newArr;}

Method 2:

/** * The two-dimensional array is sorted by a field* @param array $array array to be sorted * @param string $keys key field to sort * @param string $sort sort type SORT_ASC SORT_DESC * @return array sorted array */function arraySort($array, $keys, $sort = SORT_DESC) {$keysValue = [];foreach ($array as $k => $v) {$keysValue[$k] = $v[$keys];}array_multisort($keysValue, $sort, $array);return $array;}# sort by distance in descending order$a = arraySort($data, 'distance', SORT_DESC);print_r($a);# sort by money in ascending order$b = arraySort($data, 'money', SORT_ASC);print_r($b);
Array([0] => Array([customer_name] => Li Liangliang[money] => 45[distance] => 26[address] => Lane 198, Tianshan West Road)[1] => Array([customer_name] => Dong Juan[money] => 67[distance] => 17[address] => No. 2, Xinda South Road))

Extension:

Definition and Usage The reset() function points the internal pointer to the first element in the array and outputs it.current() – Returns the value of the current element in the array.end() – Point the internal pointer to the last element in the array, and output.next() – Point the internal pointer to the next element in the array, and output.prev() – Point the internal pointer to the previous element in the array, and output.each() – Returns the key name and key value of the current element and moves the internal pointer forward.

sort sorts the values ​​of the array in ascending order (rsort descending), does not keep the original keys ksort sorts the keys of the array in ascending order (krsort descending) retains the key-value relationship asort sorts the values ​​of the array in ascending order (arsort descending),Retain key-value relationships

array_multisort
  • Sort order flags: SORT_ASC - sort in ascending order SORT_DESC - sort in descending order
  • Sort type flags: SORT_REGULAR – compare items as usual SORT_NUMERIC – compare items by number SORT_STRING – compare items as strings Cannot specify two sort flags of the same kind after each array.The sort flags specified after each array are valid only for that array - before that were the defaults SORT_ASC and SORT_REGULAR.
$ar = array(array("b10", 'c11', 101, 100, "a"),array(1, 2, "2", 9, 5));array_multisort($ar[0], SORT_ASC, SORT_STRING, $ar[1], SORT_NUMERIC, SORT_DESC);print_r($ar);
Array([0] => Array([0] => 100[1] => 101[2] => a[3] => b10[4] => c11)[1] => Array([0] => 9[1] => 2[2] => 5[3] => 1[4] => 2))

Publisher: Full-stack programmer, please indicate the source: https://javaforall.cn/105808.htmlOriginal link: https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208092049543623.html