如何按嵌套在 PHP 数组中的元素对 PHP 数组进行排序?

发布于 2024-09-19 04:16:38 字数 334 浏览 10 评论 0原文

我有一个如下所示的数组:

Array
(
    [0] => Array
        (
            'name' => "Friday"
            'weight' => 6
        )
    [1] => Array
        (
            'name' => "Monday"
            'weight' => 2
        )
)

我想获取该数组中的最后一个值(“权重”),并使用它对主数组元素进行排序。因此,在这个数组中,我想对其进行排序,以便“星期一”元素出现在“星期五”元素之前。

I have an array like the following:

Array
(
    [0] => Array
        (
            'name' => "Friday"
            'weight' => 6
        )
    [1] => Array
        (
            'name' => "Monday"
            'weight' => 2
        )
)

I would like to grab the last values in that array (the 'weight'), and use that to sort the main array elements. So, in this array, I'd want to sort it so the 'Monday' element appears before the 'Friday' element.

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

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

发布评论

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

评论(7

花之痕靓丽 2024-09-26 04:16:48

注意,如果您要排序的元素是像 .0534 和 .0353 这样的浮点数(例如百分比),那么您必须将两者都乘以 1000。坦率地说,不确定为什么......看来 usort 似乎比较整数价值观。

我花了一段时间才弄清楚其中一个...

以及 2 个可能不会立即显而易见的提示:

  1. 如果您的数组是对象,则可以使用 return $a->weight - $b->weight 当然,
  2. 如果您返回 $ b->weight - $a->weight,它会降序排序。

NOTE, if the element you are sorting on is a float like .0534 and .0353 (like for a percentage), then you have to multiply both by 1000. not sure why frankly... it appears that usort seems to compare the integer values.

took me awhile to figure that one out...

and 2 tips that may not be immediately obvious:

  1. if your arrays are objects, you can use return $a->weight - $b->weight of course
  2. if you return $b->weight - $a->weight, it will sort desending.
云淡月浅 2024-09-26 04:16:48

这是一个很酷的函数,可能会有所帮助:

function subval_sort($a,$subkey,$sort) {
    foreach($a as $k=>$v) {
        $b[$k] = strtolower($v[$subkey]);
    }
    if($b)
    {
        $sort($b);
        foreach($b as $key=>$val) {
            $c[] = $a[$key];
        }
        return $c;
    }
}

将数组作为 $a 发送,将键作为 $subkey 发送,并为 $sort< 发送“asort”或“sort” /代码> 变量

Here's a cool function that might help:

function subval_sort($a,$subkey,$sort) {
    foreach($a as $k=>$v) {
        $b[$k] = strtolower($v[$subkey]);
    }
    if($b)
    {
        $sort($b);
        foreach($b as $key=>$val) {
            $c[] = $a[$key];
        }
        return $c;
    }
}

Send in the array as $a the key as $subkey and 'asort' or 'sort' for the $sort variable

_蜘蛛 2024-09-26 04:16:47

同意 usort,我有时也使用 array_multisort (https://www.php. net/manual/en/function.usort.php) 示例 3,对数据库结果进行排序。
你可以做类似的事情:

<?php
$days = array(
  array('name' => 'Friday', 'weight' => 6),
  array('name' => 'Monday', 'weight' => 2),
);

$weight = array();
foreach($days as $k => $d) {
  $weight[$k] = $d['weight'];
}

print_r($days);

array_multisort($weight, SORT_ASC, $days);

print_r($days);
?>

输出:

Array
(
    [0] => Array
        (
            [name] => Friday
            [weight] => 6
        )

    [1] => Array
        (
            [name] => Monday
            [weight] => 2
        )

)
Array
(
    [0] => Array
        (
            [name] => Monday
            [weight] => 2
        )

    [1] => Array
        (
            [name] => Friday
            [weight] => 6
        )

)

Agree with usort, I also sometimes use array_multisort (https://www.php.net/manual/en/function.usort.php) example 3, sorting database results.
You could do something like:

<?php
$days = array(
  array('name' => 'Friday', 'weight' => 6),
  array('name' => 'Monday', 'weight' => 2),
);

$weight = array();
foreach($days as $k => $d) {
  $weight[$k] = $d['weight'];
}

print_r($days);

array_multisort($weight, SORT_ASC, $days);

print_r($days);
?>

Output:

Array
(
    [0] => Array
        (
            [name] => Friday
            [weight] => 6
        )

    [1] => Array
        (
            [name] => Monday
            [weight] => 2
        )

)
Array
(
    [0] => Array
        (
            [name] => Monday
            [weight] => 2
        )

    [1] => Array
        (
            [name] => Friday
            [weight] => 6
        )

)
土豪 2024-09-26 04:16:47

如果您排序的字段是类似 title name 的字符串,
array_multisort + 自然排序CaseInSensitivity 是可行的方法:

$sort_by_title = array();
foreach($items as $item) {
  $sort_by_title [] = $item['title'];
}
array_multisort($sort_by_title , SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE, $items );

If the filed you sort by is string like title name,
array_multisort + Flags for Natural Sorting and CaseInSensitivity are the way to go:

$sort_by_title = array();
foreach($items as $item) {
  $sort_by_title [] = $item['title'];
}
array_multisort($sort_by_title , SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE, $items );
别念他 2024-09-26 04:16:45

您还可以使用匿名函数。

usort($items, function($a, $b) {
    return $a['name'] > $b['name'];
});

You can also use an anonymous function.

usort($items, function($a, $b) {
    return $a['name'] > $b['name'];
});
蓝礼 2024-09-26 04:16:44

您可以将 usort 用作:

function cmp($a, $b) {
   return $a['weight'] - $b['weight'];
}

usort($arr,"cmp");

You can use usort as:

function cmp($a, $b) {
   return $a['weight'] - $b['weight'];
}

usort($arr,"cmp");
柠栀 2024-09-26 04:16:44

可以使用匿名函数来完成。

另外,如果您的“权重”是字符串,请使用其他返回之一(请参阅注释掉的行):

<?php

$arr = array(
    0 => array (
        'name'   => 'Friday',
        'weight' => 6,
    ),
    1 => array (
        'name'   => 'Monday',
        'weight' => 2,
    ),
);

// sort by 'weight'
usort($arr, function($a, $b) { // anonymous function
    // compare numbers only
    return $a['weight'] - $b['weight'];

    // compare numbers or strings
    //return strcmp($a['weight'], $b['weight']);

    // compare numbers or strings non-case-sensitive
    //return strcmp(strtoupper($a['weight']), strtoupper($b['weight']));
});

var_export($arr);

/*
array (
    0 => array (
        'name'   => 'Monday',
        'weight' => 2,
    ),
    1 => array (
        'name'   => 'Friday',
        'weight' => 6,
    ),
)
*/

Can be done using an anonymous function.

Also if your 'weight' is a string use one of the other returns (see the commented out lines):

<?php

$arr = array(
    0 => array (
        'name'   => 'Friday',
        'weight' => 6,
    ),
    1 => array (
        'name'   => 'Monday',
        'weight' => 2,
    ),
);

// sort by 'weight'
usort($arr, function($a, $b) { // anonymous function
    // compare numbers only
    return $a['weight'] - $b['weight'];

    // compare numbers or strings
    //return strcmp($a['weight'], $b['weight']);

    // compare numbers or strings non-case-sensitive
    //return strcmp(strtoupper($a['weight']), strtoupper($b['weight']));
});

var_export($arr);

/*
array (
    0 => array (
        'name'   => 'Monday',
        'weight' => 2,
    ),
    1 => array (
        'name'   => 'Friday',
        'weight' => 6,
    ),
)
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文