简体   繁体   中英

Multidimensional arrays sorting by inner Multidimensional arrays key

What is the easiest way to sort the array using the time-stamp? For example; the array key 19 needs to be the first one after sorting because key 1494238836 is the highest in the inner array.

    $testArray = array (
        17 =>
              array (
                  1490011261 =>
                    (object)(array('test15')),
              ),
         1 =>
          array (
              1491898801 =>
                (object)(array('test14')),
          ),
        25 =>
        array (
            1491474454 =>
                (object)(array('test1')),
            1491474657 =>
                (object)(array('test5'))
        ),
        19 =>
            array (
                1494238836 =>
                (object)(array('test1')),
        ),


    );

So I did something like this:

    $sortArray = array();
    foreach ($testArray as $key =>$value){
        $maxKey = max(array_keys($value));
        $sortArray[$maxKey] = $key;
    }
    krsort($sortArray);
    echo "<pre>";
    var_export($sortArray);
    echo "</pre>";
    echo "<br/>";
    echo "<br/>";

    //Testing the theory
    foreach($sortArray as $key =>$value)
    {
        echo "<br/> Main Key:";
        echo $value;
        echo "<pre>";
        var_export($testArray[$value]);
        echo "</pre>";
        echo "<br/>";
        echo "___________________________";

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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