php 根据内部数组中的值对PHP多维数组进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16306416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:54:11  来源:igfitidea点击:

Sort PHP multi-dimensional array based on value in inner array?

phpsorting

提问by user1216398

I'm trying to sort my PHP hashtable based on a specific value in the inner array. The data structure looks like this:

我正在尝试根据内部数组中的特定值对我的 PHP 哈希表进行排序。数据结构如下所示:

print_r($mydata);

Array(
[0] => Array
    (
        [type] => suite
        [name] => A-Name
    )
[1] => Array
    (
        [type] => suite
        [name] => C-Name
    )
[2] => Array
    (
        [type] => suite
        [name] => B-Name
    )
)

I've tried ksort, sort, usortbut nothing seems to work. I'm trying to sort based on the name keytwo-levels down.

我试过ksortsortusort但似乎没有任何效果。我正在尝试根据名称键向下两级进行排序。

This was my attempt using usort:

这是我使用 usort 的尝试:

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

usort($mydata, "cmp");

Is there an easy way to do this or do I need to write a custom sort function?

有没有一种简单的方法可以做到这一点,或者我是否需要编写自定义排序函数?

回答by voodoo417

Thinking,more useful and practical http://php.net/manual/en/function.sort.php

思考,更实用更实用 http://php.net/manual/en/function.sort.php

function array_sort($array, $on, $order=SORT_ASC){

    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
                break;
            case SORT_DESC:
                arsort($sortable_array);
                break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}

How to use

如何使用

 $list = array(
   array( 'type' => 'suite', 'name'=>'A-Name'),
   array( 'type' => 'suite', 'name'=>'C-Name'),
   array( 'type' => 'suite', 'name'=>'B-Name')
 );

$list = array_sort($list, 'name', SORT_ASC);

array(3) { [0]=> array(2) { ["type"]=> string(5) "suite" ["name"]=> string(6) "A-Name"    } [2]=> array(2) { ["type"]=> string(5) "suite" ["name"]=> string(6) "B-Name" } [1]=> array(2) { ["type"]=> string(5) "suite" ["name"]=> string(6) "C-Name" } }

回答by Ali Seyfollahi

Try this usort function:

试试这个 usor 函数:

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

$my_array = array(
0 => array
    (
        'type' => 'suite'
        ,'name' => 'A-Name'
    )
,1 => array
    (
        'type' => 'suite'
        ,'name' => 'C-Name'
    )
,2 => array
    (
        'type' => 'suite'
        ,'name' => 'B-Name'
    )
);


usort($my_array, "cmp");

If you using it in a class, the second parameter changes to an array like this:

如果您在类中使用它,第二个参数将更改为如下所示的数组:

usort($my_array, array($this,'cmp'));

回答by Andrey Volk

array_multisort()- Sort multiple or multi-dimensional arrays

array_multisort()- 对多维或多维数组进行排序

回答by Prateek Joshi

 <?php
$a=array(
array('a'=>5,'b'=>7),array('c'=>4,'d'=>2),array('e'=>0,'f'=>12)

    );
function cmp_sort($x,$y){           //your function to compare two keys
if($x===$y)
    return 0;
else
    return ($x<$y?1:-1);
}

uasort($a,'cmp_sort');    //call user-defined compare function
print_r($a);              //printing the sorted array


?>

Output=>

输出=>

Array ( [2] => Array ( [e] => 0 [f] => 12 ) [1] => Array ( [c] => 4 [d] => 2 ) [0] => Array ( [a] => 5 [b] => 7 ) )

数组 ( [2] => 数组 ( [e] => 0 [f] => 12 ) [1] => 数组 ( [c] => 4 [d] => 2 ) [0] => 数组 ( [ a] => 5 [b] => 7 ) )

回答by pawan sen

$this->aasort($array,"key");

function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);

    foreach ($array as $ii => $va) {
        $sorter[]=$va[$key];
    }

    arsort($sorter);

    foreach ($sorter as $ii => $va) {
        $ret[]=$array[$ii];
    }

    return $array=$ret;
}

回答by Mateus Pastori

try this recursive mode, that I used in Magento REST API:

试试我在 Magento REST API 中使用的递归模式:

$parameters = ['searchCriteria' => ['currentPage' => 1, 'pageSize' => 20]];
$parameters['searchCriteria']['filter_groups'][0]['filters'][0] = ['condition_type' => 'from', 'field' => 'updated_at', 'value' => '2017-01-01T00:00:00'];
$parameters['searchCriteria']['filter_groups'][1]['filters'][0] = ['condition_type' => 'to', 'field' => 'updated_at', 'value' => '2017-12-31T23:59:59']; 

function sortArrayByKeyAsc($_params){
    if(is_array($_params)){
        uksort($_params, 'strnatcmp');
        foreach ($_params as $key => $value){
            if(is_array($value)){
                $_params[$key] = sortArrayByKeyAsc($value);
            }
        }
    }
    return $_params;
}