排序Mulitdemensional阵列PHP [英] Sorting Mulitdemensional Array PHP

查看:121
本文介绍了排序Mulitdemensional阵列PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的阵列,它目前的创entity_count排序。(在CakePHP中做了查询输出 - 我只是想排在前几位的实体),我想现在对于实体 - 阵列>标题排序

我试图与在array_multisort 这样做,但失败了。这可能吗?

 阵列

    [0] =>排列
        (
            [实体] =>排列
                (
                    [标题] =>橙子
                )            [0] =>排列
                (
                    [entitycount] => 76
                )        )    [1] =>排列
        (
            [实体] =>排列
                (
                    [标题] =>苹果
                )            [0] =>排列
                (
                    [entitycount] => 78
                )
        )
    [2] =>排列
        (
            [实体] =>排列
                (
                    [标题] =>柠檬
                )            [0] =>排列
                (
                    [entitycount] => 85
                )
        )


解决方案

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

 回调函数($值)
{
    返回使用isset($值['实体'] ['标题'])? $值['实体'] ['标题']:空;
}

然后运行它thew一个array_map和多类

 在array_multisort(array_map($ myArray的,'回调'),$ myArray的);

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
                )
        )
)

解决方案

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);

这篇关于排序Mulitdemensional阵列PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆