PHP 多维数组排序

发布于 2024-09-25 06:16:21 字数 977 浏览 2 评论 0原文

我有以下数组,它当前是按实体计数排序的(由在 cakephp 中完成的查询输出 - 我只想要前几个实体),我现在想对 Entity->title 的数组进行排序。

我尝试使用array_multisort执行此操作,但失败了。这可能吗?

Array
(
    [0] => Array
        (
            [Entity] => Array
                (
                    [title] => Orange
                )

            [0] => Array
                (
                    [entitycount] => 76
                )

        )

    [1] => Array
        (
            [Entity] => Array
                (
                    [title] => Apple
                )

            [0] => Array
                (
                    [entitycount] => 78
                )
        )
    [2] => Array
        (
            [Entity] => Array
                (
                    [title] => Lemon
                )

            [0] => Array
                (
                    [entitycount] => 85
                )
        )
)

I have the following array, it is currently created sorted by entity_count (outputted by a query done in cakephp - I only wanted the top few entities), I want to now sort the array for the Entity->title.

I tried doing this with array_multisort but failed. Is this possible?

Array
(
    [0] => Array
        (
            [Entity] => Array
                (
                    [title] => Orange
                )

            [0] => Array
                (
                    [entitycount] => 76
                )

        )

    [1] => Array
        (
            [Entity] => Array
                (
                    [title] => Apple
                )

            [0] => Array
                (
                    [entitycount] => 78
                )
        )
    [2] => Array
        (
            [Entity] => Array
                (
                    [title] => Lemon
                )

            [0] => Array
                (
                    [entitycount] => 85
                )
        )
)

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

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

发布评论

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

评论(3

生生漫 2024-10-02 06:16:21

像这样创建一个回调函数:

function callback($value)
{
    return isset($value['entity']['title']) ? $value['entity']['title'] : null;
}

然后运行 ​​array_map 和多重排序

array_multisort(array_map($myArray,'callback'), $myArray);

Create a callback function like so:

function callback($value)
{
    return isset($value['entity']['title']) ? $value['entity']['title'] : null;
}

Then run it thew an array_map and multi sort

array_multisort(array_map($myArray,'callback'), $myArray);
情何以堪。 2024-10-02 06:16:21

试试这个:

$keys = array_map($arr, function($val) { return $val['Entity']['title']; });
array_multisort($keys, $arr);

这里 array_maparray_map net/functions.anonymous" rel="nofollow">匿名函数(自 PHP 5.3 起可用,您可以使用 create_function(在之前的版本中)用于获取标题数组,然后用于根据标题对数组进行排序。

Try this:

$keys = array_map($arr, function($val) { return $val['Entity']['title']; });
array_multisort($keys, $arr);

Here array_map and an anonymous function (available since PHP 5.3, you can use create_function in prior versions) is used to get an array of the titles that is then used to sort the array according to their titles.

谎言月老 2024-10-02 06:16:21

您需要编写一个自定义比较函数,然后使用 usort
使用以下方式调用它:

usort  ( $arrayy  , callback $cmp_function  );

You need to write a custom compare function and then use usort .
Calll it using:

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