按一列的值对二维数组进行排序

发布于 2024-09-19 04:53:38 字数 499 浏览 2 评论 0原文

我在思考如何做到这一点时遇到了一些问题。 我在 PHP 中有一个数组

    array(131) {
     ["BLANF     "]=>
      array(3) {
        ["line_3"]=>
        string(4) "3.92"
        ["line_1"]=>
        string(1) "6"
        ["line_2"]=>
        string(2) "14"
      }
      ["BLOOH     "]=>
      array(3) {
        ["line_3"]=>
        string(4) "2.00"
        ["line_1"]=>
        string(1) "20"
        ["line_2"]=>
        string(1) "6"
      }

}

,需要根据 line_1 的值进行排序。

I'm having some problems wrapping my head around how to do this.
I have an array in PHP

    array(131) {
     ["BLANF     "]=>
      array(3) {
        ["line_3"]=>
        string(4) "3.92"
        ["line_1"]=>
        string(1) "6"
        ["line_2"]=>
        string(2) "14"
      }
      ["BLOOH     "]=>
      array(3) {
        ["line_3"]=>
        string(4) "2.00"
        ["line_1"]=>
        string(1) "20"
        ["line_2"]=>
        string(1) "6"
      }

}

That I need to sort based on the value of line_1.

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

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

发布评论

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

评论(2

山有枢 2024-09-26 04:53:38

您必须为数组创建一个自定义比较函数,并将其与 uasort() 维护数组的索引。

以下是如何使用 uasort()line_1 排序...更改为按嵌套数组中的任何其他键排序很简单。

<?php

  // The custom comparison function
function cmp($a, $b)            
{
    if ($a["line_1"] == $b["line_1"]) {
        return 0;
    }
    return ($a["line_1"] < $b["line_1"]) ? -1 : 1;
}

  // Sort the array using your custom comparison
uasort($array, 'cmp');

  // Make sure we got the right result
print_r($array);
?>

实时示例

(我更改了 line_1 数字,以便排序实际上做了一些事情)

在这种情况下,PHP 会为您处理类型,但您应该注意您有字符串并将它们转换为数字的事实。如果您不确定会发生什么,请将字符串转换为浮点数或整数。这很重要,因为 PHP 可以使用比较运算符按字母顺序比较字符串....所以,如果字母或逗号或其他内容有可能潜入您的数组值,那么您可以 类型转换 为 int ( (int) $a["line_1"] ) 或 float ( (float) $a["line_1"] )。

You have to create a custom comparison function for your array and employ it with uasort() to maintain the indices of the array.

Here is how you can use uasort() to sort by line_1... It's simple to change to sort by any other key in the nested array.

<?php

  // The custom comparison function
function cmp($a, $b)            
{
    if ($a["line_1"] == $b["line_1"]) {
        return 0;
    }
    return ($a["line_1"] < $b["line_1"]) ? -1 : 1;
}

  // Sort the array using your custom comparison
uasort($array, 'cmp');

  // Make sure we got the right result
print_r($array);
?>

Live Example

(I changed the line_1 numbers so that the sort actually does something)

In this case PHP will juggle the types for you, but you should watch out for the fact that you have strings and are converting them to numbers. If you're not sure what will happen then cast the strings to floats or ints. This is important since, PHP can compare strings alphabetically with the comparison operators.... so, if there's any chance that a letter or comma or something can sneak into your array value then you can type cast to an int ( (int) $a["line_1"] ) or a float ( (float) $a["line_1"] ).

念三年u 2024-09-26 04:53:38

通过提供比较回调函数来使用 uasort比较相应行的值。

Use uasort by providing a comparison callback function that compares the values of the appropriate lines.

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