简体   繁体   English

PHP-排序多维数组

[英]PHP - Sorting Multidimensional Array

I am trying to arrange my array (below) by [date], but to no avail as of yet :( 我想在[日期]之前安排我的数组(如下),但到目前为止无济于事:(

Needed to pad this out with some more text as apparently it's mostly code and stackoverflow doesn't like this :/ 需要用更多的文本来填充它,因为显然这主要是代码,并且stackoverflow不喜欢这样:/

Array
(
    [0] => gapiReportEntry Object
        (
            [metrics:gapiReportEntry:private] => Array
                (
                    [uniquepageviews] => 0
                    [pageviews] => 0
                    [visits] => 0
                    [visitors] => 0
                )

            [dimensions:gapiReportEntry:private] => Array
                (
                    [date] => 20131009
                )

        )

    [1] => gapiReportEntry Object
        (
            [metrics:gapiReportEntry:private] => Array
                (
                    [uniquepageviews] => 1
                    [pageviews] => 1
                    [visits] => 1
                    [visitors] => 1
                )

            [dimensions:gapiReportEntry:private] => Array
                (
                    [date] => 20131026
                )

        )
)

Can anyone help me? 谁能帮我? Thanks, 谢谢,

You can use standard php usort function to do it : 您可以使用标准的PHP usort函数来做到这一点:

$array = usort($array, function ($a, $b) use ($array){
    return strcmp($a -> dimensions -> date,  $b -> dimensions -> date);
}):

Notice I use a closure here (see http://php.net/manual/fr/function.usort.php )) 请注意,我在这里使用了闭包(请参阅http://php.net/manual/fr/function.usort.php ))

Considering the array is called $array, try this code: 考虑到数组称为$ array,请尝试以下代码:

$dates = array();
foreach($array as $v)
    $dates[] = $v['dimensions:gapiReportEntry:private'];

asort($dates);

$sorted_array = array();
foreach($dates as $k => $v)
    $sorted_array[$k] = $array[$k];

You will have the result in the $sorted_array variable. 您将在$ sorted_array变量中得到结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM