使用 usort 对数组进行排序?

发布于 2024-12-15 04:53:53 字数 610 浏览 1 评论 0原文

我在尝试对数组进行排序时遇到问题 - 我希望所有 IsOpen = 1 出现在顶部?

示例:

Array
(
    [0] => Array
        (
            [Isopen] => 0
        )

    [2] => Array
        (
            [Isopen] => 1
        )

    [3] => Array
        (
            [Isopen] => 0
        )

    [4] => Array
        (
            [Isopen] => 1
        )

代码:

   function cmp($a, $b) {
        if ($a['Isopen'] >= $b['Isopen']) {
            return 0;
        }
    }

    usort($data['rowResult'], "cmp");

我不明白 $a 和 $b 是什么意思,我查看了 PHP 文档 - 信息不清楚。

I am having problem trying to sort an array - I want all the IsOpen = 1 to appear at the top?

Example:

Array
(
    [0] => Array
        (
            [Isopen] => 0
        )

    [2] => Array
        (
            [Isopen] => 1
        )

    [3] => Array
        (
            [Isopen] => 0
        )

    [4] => Array
        (
            [Isopen] => 1
        )

Code:

   function cmp($a, $b) {
        if ($a['Isopen'] >= $b['Isopen']) {
            return 0;
        }
    }

    usort($data['rowResult'], "cmp");

I don't understand what does $a and $b mean, I had a look at the PHP documentation - information is not clear.

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

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

发布评论

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

评论(6

云巢 2024-12-22 04:53:53

$a 和 $b 是要比较的元素:

function cmp($a, $b) {
    if ($a['Isopen'] == $b['Isopen']) {
        return 0;
    }
    return ($a['Isopen'] > $b['Isopen']) ? -1 : 1;
}

$a and $b are the elements to compare:

function cmp($a, $b) {
    if ($a['Isopen'] == $b['Isopen']) {
        return 0;
    }
    return ($a['Isopen'] > $b['Isopen']) ? -1 : 1;
}
擦肩而过的背影 2024-12-22 04:53:53

cmp 函数应返回 0、-1(或小于零)或 1(或大于零)。

  • 如果两个值相等则为零。
  • 如果 a 的值较高,则大于零。
  • 如果 b 的值较高,则小于零。

为了比较两个值,您可以使用 php 函数 strcmp

function cmp($a, $b) {
  return strcmp($a['Isopen'], $b['Isopen']);
}

The cmp funciton should return 0, -1 (or less than zero) or 1 (or greater than zero).

  • zero if the two values are equal.
  • greater than zero if the value form a is higher.
  • less than zero if the value from b is higher.

for the comparison of the two values you may use php function strcmp

function cmp($a, $b) {
  return strcmp($a['Isopen'], $b['Isopen']);
}
才能让你更想念 2024-12-22 04:53:53
function cmp($a, $b) {
        if ($a['Isopen'] == $b['Isopen']) {
            return 0;
        }
        return ($a['Isopen'] >  $b['Isopen']) ? -1 : 1;
    }

这将使 Isopen = 1 在数组的末尾,如果你想在乞求它

 function cmp($a, $b) {
            if ($a['Isopen'] == $b['Isopen']) {
                return 0;
            }
            return ($a['Isopen'] <  $b['Isopen']) ? -1 : 1;
        }
function cmp($a, $b) {
        if ($a['Isopen'] == $b['Isopen']) {
            return 0;
        }
        return ($a['Isopen'] >  $b['Isopen']) ? -1 : 1;
    }

this will makes Isopen = 1 at the end of the array if you want it at the begging make this

 function cmp($a, $b) {
            if ($a['Isopen'] == $b['Isopen']) {
                return 0;
            }
            return ($a['Isopen'] <  $b['Isopen']) ? -1 : 1;
        }
ゃ人海孤独症 2024-12-22 04:53:53

在此示例中,$a[] 和 $b[] 是传递给名为“cmp”的函数的两个数组。它比较两个不同数组中“Isopen”的值。

查看有关用户定义函数的文档
http://php.net/manual/en/language.functions.php

以及如何正确使用 usort()
http://php.net/manual/en/function.usort.php

in this example $a[] and $b[] are two arrays that are handed to the function named 'cmp'. it compares the value of 'Isopen' in two different arrays.

check out the documentation on user defined functions
http://php.net/manual/en/language.functions.php

and how to properly use usort()
http://php.net/manual/en/function.usort.php

誰ツ都不明白 2024-12-22 04:53:53

排序算法是冒泡排序

本质上,它一次比较数组实体以确定它们的顺序。要查看分步示例,请参阅 http://en.wikipedia.org/ wiki/Bubble_sort#Step-bystep_example

The sorting aglorithm is a Bubble Sort.

Essentially, it compares array entities at a time to determining their ordering. To see a step by step example, see http://en.wikipedia.org/wiki/Bubble_sort#Step-by-step_example

ぇ气 2024-12-22 04:53:53

您的函数 cmp 不正确(请参阅我的正确使用示例)。此外,如果你使用 php 5.3+,你可以使用这样的匿名函数:

usort($data['rowResult'], function($a, $b)
{
    if ($a['Isopen'] == $b['Isopen'])
    {
        return 0;
    }

    return $a['Isopen'] < $b['Isopen'] ? -1 : 1;
});

Your function cmp is not correct (see my example of correct use). Besides if you use php 5.3+ you can use anonymous functions like this:

usort($data['rowResult'], function($a, $b)
{
    if ($a['Isopen'] == $b['Isopen'])
    {
        return 0;
    }

    return $a['Isopen'] < $b['Isopen'] ? -1 : 1;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文