当前位置:网站首页>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
边栏推荐
猜你喜欢

APP automation test framework - UiAutomator2 introductory

论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》

威纶通触摸屏制作自定义弹出窗口的具体方法(3种)

hdu 1503 Advanced Fruits(最长公共子序列的应用)
6个规则去净化你的代码

小黑leetcode之旅:94. 二叉树的中序遍历(补充Morris 中序遍历)

浅谈Numpy中的shape、reshape函数的区别

微软word怎么转换成pdf文件?微软word转换为pdf格式的方法

Pagoda measurement - building LightPicture open source map bed system

PMP每日一练 | 考试不迷路-8.9(包含敏捷+多选)
随机推荐
Skywalking系列学习之Trace Profiling源码分析
10个 Istio 流量管理 最常用的例子,你知道几个?
hdu 1333 Smith Numbers(暴力思路)
leetcode: the Kth largest element in the array
Sudoku | Backtrack-7
[Generic Programming] Full Detailed Explanation of Templates
Tensorflow中使用convert_to_tensor去指定数据的类型
场效应管Mosfet之雷卯Leiditech对应英飞凌Infineon
什么是IDE(集成开发环境)?
小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和
Deceptive Dice(期望计算)
Byte side: Can TCP and UDP use the same port?
PHP 二维数组根据某个字段排序
Ehrlich screening method: Counting the number of prime numbers
几种绘制时间线图的方法
Tensorflow模型整体构建流程
NetCore路由的Endpoint模式
Synchronization lock synchronized traces the source
LED闪烁 闪灯芯片IC 手电筒IC 闪灯控制IC 闪烁IC流水灯
TF generates uniformly distributed tensor