当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Optimization of SQL Statements and Indexes
蓝牙模块有哪些种类?BLE低功耗蓝牙模块有什么特点?
技术分享 | 接口自动化测试如何处理 Header cookie
Daily practice of PMP | Do not get lost in the exam -8.8 (including agility + multiple choice)
Jensen (琴生) 不等式
TF generates uniformly distributed tensor
Technology Sharing | How to use the JSON Schema mode of interface automation testing?
SecureCRT 设置超时自动断开连接时长
cad图纸怎么复制到word文档里面?Word里插CAD图怎么弄?
SecureCRT强制卸载
数独 | 回溯-7
MySQL:错误1153(08S01):得到的数据包大于“ max_allowed_packet”字节
小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和
万字总结:分布式系统的38个知识点
SQLi-LABS Page-2 (Adv Injections)
Sudoku | Backtrack-7
微软Excel表格点击单元格行和列都显示颜色怎么弄?聚光灯效果设置
NIO Cup 2022 Nioke Summer Multi-School Training Camp 7 CFGJ
Use convert_to_tensor in Tensorflow to specify the type of data
mysql配置参数详解[通俗易懂]









