按子元素对数组进行排序的最有效方法?

发布于 2024-09-18 21:58:00 字数 223 浏览 2 评论 0原文

我有一个多维数组。

$array[0] = array(1, 8, 2);    
$array[1] = array(5, 6, 15);
$array[2] = array(-8, 2, 1025);

我想知道通过子数组的特定属性对父数组进行排序的最有效方法是什么。例如,我想将它们按 $sub_array[1] 的升序排列,因此父数组将按 2,1,0 排序。

I have a multidimensional array.

$array[0] = array(1, 8, 2);    
$array[1] = array(5, 6, 15);
$array[2] = array(-8, 2, 1025);

I am wondering what the most efficient way to order the parent array by a particular property of it's sub array. For example, I want to put them in ascending order of $sub_array[1], so the parent array would be ordered 2,1,0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱影子 2024-09-25 21:58:00

sort 及其同类有一些变体,其中您可以提供自己的排序回调函数: usort< /a>, uasort (其中维护索引)和 uksort (您必须创建自己的排序回调来执行您想要执行的操作,

function sort_by_subarray($a, $b)
{
  // $a and $b are elements of $array that are being compared against
  // each other to be sorted

  if (!isset($a[1]) || !isset($b[1]))
  {
    // Do something if $a[1] or $b[1] is undefined
    // Your question doesn't define what the behaviour here should be
  }
  else
  {
    if ($a[1] == $b[1]) return 0;     // the elements are the same
    return ($a[1] < $b[1]) ? -1 : 1;  // otherwise, sort from lowest to highest
  }
}

$array = array(
  array(1, 8, 2),
  array(5, 6, 15),
  array(-8, 2, 1025)
);
uasort($array, "sort_by_subarray");

/* Results in:
Array
(
    [2] => Array
        (
            [0] => -8
            [1] => 2
            [2] => 1025
        )

    [1] => Array
        (
            [0] => 5
            [1] => 6
            [2] => 15
        )

    [0] => Array
        (
            [0] => 1
            [1] => 8
            [2] => 2
        )

)
*/

请注意,如果 $subarray[1] 相等,那么我的函数会将两个子数组排序为相等。更具体地说,您可以为 $a[1] == $b[1] 添加更多规则。

sort and its cousins have variations where you can supply your own sorting callback function: usort, uasort (which maintains indexes), and uksort (which sorts on the keys. You'll have to create your own sorting callback to do what you want to do here.

function sort_by_subarray($a, $b)
{
  // $a and $b are elements of $array that are being compared against
  // each other to be sorted

  if (!isset($a[1]) || !isset($b[1]))
  {
    // Do something if $a[1] or $b[1] is undefined
    // Your question doesn't define what the behaviour here should be
  }
  else
  {
    if ($a[1] == $b[1]) return 0;     // the elements are the same
    return ($a[1] < $b[1]) ? -1 : 1;  // otherwise, sort from lowest to highest
  }
}

$array = array(
  array(1, 8, 2),
  array(5, 6, 15),
  array(-8, 2, 1025)
);
uasort($array, "sort_by_subarray");

/* Results in:
Array
(
    [2] => Array
        (
            [0] => -8
            [1] => 2
            [2] => 1025
        )

    [1] => Array
        (
            [0] => 5
            [1] => 6
            [2] => 15
        )

    [0] => Array
        (
            [0] => 1
            [1] => 8
            [2] => 2
        )

)
*/

Note that my function will sort two subarrays as being equal if $subarray[1] is equal, so if you want to be more specific, you can add more rules for when $a[1] == $b[1].

燃情 2024-09-25 21:58:00

使用 http://www.php.net/manual/en/function.usort .php 并编写一个回调函数来实现您想要的排序条件。

Use http://www.php.net/manual/en/function.usort.php and write a callback function that implements the sort condition you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文