当前位置:网站首页>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
边栏推荐
- 技术分享 | 接口自动化测试如何处理 Header cookie
- 4D Summary: 38 Knowledge Points of Distributed Systems
- TF中使用zeros(),ones(), fill()方法生成数据
- 【stack】【queue】【priority_queue】【deque】详解
- DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine reduce the immunogenicity of peptides
- random.normal() and random.truncated_normal() in TF
- leetcode:数组中的第K个最大元素
- 数独 | 回溯-7
- Simple questions peek into mathematics
- Word怎么制作一张标准的答题卡?
猜你喜欢
随机推荐
Unity2D_线框材质
The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
Wps下划线怎么弄?Wps添加下划线的最全方法
np中的round函数,ceil函数与floor函数
SecureCRT背景配色
Word怎么设置图片衬于文字下方?两种方法教你设置Word图片衬于文字下方
Use zeros(), ones(), fill() methods to generate data in TF
json事例
单元测试
宝塔实测-搭建LightPicture开源图床系统
TF生成均匀分布的tensor
[Graphic and textual] How to reinstall Win7 system
Excel如何打出正负号?Excel打出正负号的方法
fixed investment fund
leetcode:数组中的第K个最大元素
Synchronization lock synchronized traces the source
6个规则去净化你的代码
字符串哈希(2014 SERC J题)
SQLi-LABS Page-2 (Adv Injections)
如何让您的公司内容满足 GDPR 合规性