当前位置:网站首页>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
边栏推荐
- 万字总结:分布式系统的38个知识点
- 6个规则去净化你的代码
- Puyuan Jingdian turned losses into profits in the first half of the year, and high-end products continued to develop!Are you optimistic about "Huawei" in the instrument industry?
- Jmeter 使用正则表达式提取器将返回值全部保存到一个文件中
- Ehrlich screening method: Counting the number of prime numbers
- Converting angles to radians
- TF uses constant to generate data
- 【双链表增删查改接口的实现】
- 单元测试
- Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
猜你喜欢
Puyuan Jingdian turned losses into profits in the first half of the year, and high-end products continued to develop!Are you optimistic about "Huawei" in the instrument industry?
[Generic Programming] Full Detailed Explanation of Templates
MySQL慢查询的多个原因
Lyapp exponents and bifurcation diagrams for fractional chaotic systems
Wps下划线怎么弄?Wps添加下划线的最全方法
Word文档怎么输入无穷大符号∞
PMP daily practice | didn't lost a 8.9 (including agile + multi-select)
SQLi-LABS Page-2 (Adv Injections)
TF生成均匀分布的tensor
PMP每日一练 | 考试不迷路-8.9(包含敏捷+多选)
随机推荐
The round functions in the np, ceil function and floor function
哪款C语言编译器(IDE)适合初学者?
Bean life cycle
Unity2D_背景粒子效果
Don't tell me to play, I'm taking the PMP exam: what you need to know about choosing an institution for the PMP exam
TF中使用zeros(),ones(), fill()方法生成数据
【stack】【queue】【priority_queue】【deque】Detailed explanation
场效应管Mosfet之雷卯Leiditech对应英飞凌Infineon
STC8H开发(十五): GPIO驱动Ci24R1无线模块
Use convert_to_tensor in Tensorflow to specify the type of data
POWER SOURCE ETA埃塔电源维修FHG24SX-U概述
The overall construction process of the Tensorflow model
在VMware上安装win虚拟机
蔚来杯2022牛客暑期多校训练营7 CFGJ
LeetCode26: remove duplicates in sorted array
同步锁synchronized追本溯源
Referenced file contains errors 完美解决方法
Simple questions peek into mathematics
蓝牙模块的分类和对应的属性特点
TF使用constant生成数据