百科狗-知识改变命运!
--

asort() - 对数组进行排序并保持索引关系 - php 数组函数

乐乐1年前 (2023-11-21)阅读数 13#技术干货
文章标签数组

asort()

(PHP 4, PHP 5, PHP 7)

对数组进行排序并保持索引关系

说明

asort(array &$array[,int $sort_flags= SORT_REGULAR]): bool

本函数对数组进行排序,数组的索引保持和单元的关联。主要用于对那些单元顺序很重要的结合数组进行排序。

Note:

If two members compare as equal, their relative order in the sorted array is undefined.

参数

$array

输入的数组。

$sort_flags

asort() - 对数组进行排序并保持索引关系 - php 数组函数

可以用可选的参数$sort_flags改变排序的行为,详情见sort()。

返回值

成功时返回TRUE,或者在失败时返回FALSE

范例

Example #1asort()例子

以上例程会输出:

c = apple
b = banana
d = lemon
a = orange

fruits 被按照字母顺序排序,并且单元的索引关系不变。

参见

  • arsort() 对数组进行逆向排序并保持索引关系
  • 数组排序函数对比
This function can be used to sort multidimensional arrays with almost no work whatsoever by using the individual values within the custom sort function.
This function passes the entire child element even if it is not a string. If it is an array, as would be the case in multidimensional arrays, it will pass the whole child array as one parameter.
Therefore, do something elegant like this:

This does in 4 lines what other functions took 40 to 50 lines to do. This does not require you to create temporary arrays or anything. This is, for me, a highly preferred solution.
Hope it helps!
For a recent project I needed to sort an associative array by value first, and then by key if a particular value appeared multiple times. I wrote this function to accomplish the task. Note that the parameters default to sort ascending on both keys and values, but allow granular control over each. 
This is a function to sort an indexed 2D array by a specified sub array key, either ascending or descending.
It is usefull for sorting query results from a database by a particular field after the query has been returned
This function can be quite greedy. It recreates the array as a hash to use ksort() then back again
By default it will sort ascending but if you specify $reverse as true it will return the records sorted descending 

Example Outputs:
Before: Array
(
  [0] => Array ( [code] => LHR, [name] => Heathrow )
  [1] => Array ( [code] => LGW, [name] => Gatwick )
)
After: Array
(
  [0] => Array ( [code] => LGW, [name] => Gatwick )
  [1] => Array ( [code] => LHR, [name] => Heathrow )
)
This is a fixed version of the same function I posted below. Now it will handle duplicate entries in the sorted field. EG: If there were two records that had the name Heathrow it would still work. 
Small typo in the aksort function I just submitted. Here's the entire thing again, with the correction noted: 
asort has one anoying feature, it ignores any default or implicit order in the data. i.e. if two elements of an array contain "banana" then it is not garanteed that the first will still be the first after the sort.
This makes the Burrows-Wheeler block sort a bit of a pain to impliment, with a trailing string having to be appended to all strings before sorting, and removed after sorting. To maintain the so called "banana" order.
acecream's function works fine, especially with the spectre extension.
nevertheless sometimes the index values have to be kept. To achieve this, just replace:
$sorted_arr[] = $array[$arr_key]; 
with:
$sorted_arr[$arr_key] = $array[$arr_key];
Advanced sort array by second index function, which produces ascending (default) or descending output and uses optionally natural case insensitive sorting (which can be optionally case sensitive as well).
Only the first two arguments are required. 
Ok I was mistaken, after re-reading the previous post the ".$key" is important. What caused the sorting issue for me wasn't that at all. But rather something else.
doing an: asort($records, $flags); before returning fixes the sorting problems.
The sorting problem I was refearing to causes a character based sorting done on numeric data.
so instead of:
1
2
3
...
10
12
20
It was returned back as
1
10
12
2
20
3
...
basically what I was trying to fix in the first place.
Here's my version of sorting multi-dimensional array by 2nd index.
Feel free to change the code to suit your needs. 
The asortbyindex($sortarray, $index) looks like sort not asort. The key of the $sortarray was changed.
Similar to above but for an array of arrays instead of an array of objects.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)