How can I sort this array by the value of the "order" key? Even though the values are currently sequential, they will not always be.
如何根据“order”键的值对这个数组进行排序?尽管这些值目前是连续的,但它们并不总是连续的。
Array
(
[0] => Array
(
[hashtag] => a7e87329b5eab8578f4f1098a152d6f4
[title] => Flower
[order] => 3
)
[1] => Array
(
[hashtag] => b24ce0cd392a5b0b8dedc66c25213594
[title] => Free
[order] => 2
)
[2] => Array
(
[hashtag] => e7d31fc0602fb2ede144d18cdffd816b
[title] => Ready
[order] => 1
)
)
7 个解决方案
#1
1360
Try a usort
: If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:
尝试使用usort:如果你还在使用PHP 5.2或更早的版本,你必须先定义一个排序函数:
function sortByOrder($a, $b) {
return $a['order'] - $b['order'];
}
usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
从PHP 5.3开始,您可以使用匿名函数:
usort($myArray, function($a, $b) {
return $a['order'] - $b['order'];
});
And finally with PHP 7 you can use the "spaceship operator":
最后,使用PHP 7,您可以使用“太空船操作员”:
usort($myArray, function($a, $b) {
return $a['order'] <=> $b['order'];
});
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.
若要将其扩展到多维排序,请参考第二个/第三个排序元素(如果第一个元素为零)——最好解释如下。你也可以用这个来分类子元素。
usort($myArray, function($a, $b) {
$retval = $a['order'] <=> $b['order'];
if ($retval == 0) {
$retval = $a['suborder'] <=> $b['suborder'];
if ($retval == 0) {
$retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];
}
}
return $retval;
});
If you need to retain key associations, use uasort()
- see comparison of array sorting functions in the manual
如果需要保留键关联,请使用uasort()—请参阅手册中数组排序函数的比较
#2
265
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
aasort($your_array,"order");
#3
242
I use this function :
我使用这个函数:
function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
$sort_col = array();
foreach ($arr as $key=> $row) {
$sort_col[$key] = $row[$col];
}
array_multisort($sort_col, $dir, $arr);
}
array_sort_by_column($array, 'order');
#4
64
I usually use usort, and pass my own comparison function. In this case, it is very simple:
我通常使用usort,通过我自己的比较函数。在这种情况下,很简单:
function compareOrder($a, $b)
{
return $a['order'] - $b['order'];
}
usort($array, 'compareOrder');
#5
13
$sort = array();
$array_lowercase = array_map('strtolower', $array_to_be_sorted);
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $alphabetically_ordered_array);
This takes care of both upper and lower case alphabets.
这将同时处理大小写字母。
#6
3
To sort the array by the value of the "title" key use:
按“标题”键的值对数组进行排序:
uasort($myArray, function($a, $b) {
return strcmp($a['title'], $b['title']);
});
strcmp compare the strings.
比较字符串的字符串进行比较。
uasort() maintains the array keys as they were defined.
uasort()按照定义维护数组键。
#7
0
Let's face it: php does NOT have a simple out of the box function to properly handle every array sort scenario.
让我们面对它:php并没有一个简单的盒子函数来正确处理每个数组排序场景。
This routine is intuitive, which means faster debugging and maintenance:
这个例程是直观的,意味着更快的调试和维护:
// automatic population of array
$tempArray = array();
$annotations = array();
// ... some code
// SQL $sql retrieves result array $result
// $row[0] is the ID, but is populated out of order (comes from
// multiple selects populating various dimensions for the same DATE
// for example
while($row = mysql_fetch_array($result)) {
$needle = $row[0];
arrayIndexes($needle); // create a parallel array with IDs only
$annotations[$needle]['someDimension'] = $row[1]; // whatever
}
asort($tempArray);
foreach ($tempArray as $arrayKey) {
$dataInOrder = $annotations[$arrayKey]['someDimension'];
// .... more code
}
function arrayIndexes ($needle) {
global $tempArray;
if (!in_array($needle,$tempArray)) {
array_push($tempArray,$needle);
}
}
#1
1360
Try a usort
: If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:
尝试使用usort:如果你还在使用PHP 5.2或更早的版本,你必须先定义一个排序函数:
function sortByOrder($a, $b) {
return $a['order'] - $b['order'];
}
usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
从PHP 5.3开始,您可以使用匿名函数:
usort($myArray, function($a, $b) {
return $a['order'] - $b['order'];
});
And finally with PHP 7 you can use the "spaceship operator":
最后,使用PHP 7,您可以使用“太空船操作员”:
usort($myArray, function($a, $b) {
return $a['order'] <=> $b['order'];
});
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.
若要将其扩展到多维排序,请参考第二个/第三个排序元素(如果第一个元素为零)——最好解释如下。你也可以用这个来分类子元素。
usort($myArray, function($a, $b) {
$retval = $a['order'] <=> $b['order'];
if ($retval == 0) {
$retval = $a['suborder'] <=> $b['suborder'];
if ($retval == 0) {
$retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];
}
}
return $retval;
});
If you need to retain key associations, use uasort()
- see comparison of array sorting functions in the manual
如果需要保留键关联,请使用uasort()—请参阅手册中数组排序函数的比较
#2
265
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
aasort($your_array,"order");
#3
242
I use this function :
我使用这个函数:
function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
$sort_col = array();
foreach ($arr as $key=> $row) {
$sort_col[$key] = $row[$col];
}
array_multisort($sort_col, $dir, $arr);
}
array_sort_by_column($array, 'order');
#4
64
I usually use usort, and pass my own comparison function. In this case, it is very simple:
我通常使用usort,通过我自己的比较函数。在这种情况下,很简单:
function compareOrder($a, $b)
{
return $a['order'] - $b['order'];
}
usort($array, 'compareOrder');
#5
13
$sort = array();
$array_lowercase = array_map('strtolower', $array_to_be_sorted);
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $alphabetically_ordered_array);
This takes care of both upper and lower case alphabets.
这将同时处理大小写字母。
#6
3
To sort the array by the value of the "title" key use:
按“标题”键的值对数组进行排序:
uasort($myArray, function($a, $b) {
return strcmp($a['title'], $b['title']);
});
strcmp compare the strings.
比较字符串的字符串进行比较。
uasort() maintains the array keys as they were defined.
uasort()按照定义维护数组键。
#7
0
Let's face it: php does NOT have a simple out of the box function to properly handle every array sort scenario.
让我们面对它:php并没有一个简单的盒子函数来正确处理每个数组排序场景。
This routine is intuitive, which means faster debugging and maintenance:
这个例程是直观的,意味着更快的调试和维护:
// automatic population of array
$tempArray = array();
$annotations = array();
// ... some code
// SQL $sql retrieves result array $result
// $row[0] is the ID, but is populated out of order (comes from
// multiple selects populating various dimensions for the same DATE
// for example
while($row = mysql_fetch_array($result)) {
$needle = $row[0];
arrayIndexes($needle); // create a parallel array with IDs only
$annotations[$needle]['someDimension'] = $row[1]; // whatever
}
asort($tempArray);
foreach ($tempArray as $arrayKey) {
$dataInOrder = $annotations[$arrayKey]['someDimension'];
// .... more code
}
function arrayIndexes ($needle) {
global $tempArray;
if (!in_array($needle,$tempArray)) {
array_push($tempArray,$needle);
}
}