1

How do I sort 3D arrays ? In this case I want to sort after date. Thx ?

array(2) { 
    ["garsoniere"]=>array(2) { 
          [0]=> array(5) {
              ["date"]=> string(19) "2014-02-04" 
              ["sponsored"]=> string(1) "0" 
              ["offer_status"]=> string(6) "active" 
              ["suprafata"]=> string(3) "111" 
              ["confort"]=> string(2) "-1" 
              ["title"]=> string(18) "Jimmy Humununukaua" 
         } 
         [1]=> array(5) { 
             ["date"]=> string(19) "2013-03-25" 
             ["sponsored"]=> string(1) "0" 
             ["offer_status"]=> NUL
             ["suprafata"]=> string(2) "23" 
             ["confort"]=> NULL 
             ["title"]=> string(38) "Garsoniera de vanzare in Marasti, Cluj" 
        } 
    } 
    ["apartamente"]=> array(2) { 
            [0]=> array(5) { 
                ["date"]=> string(19) "2014-02-05" 
                ["sponsored"]=> string(1) "0" 
                ["offer_status"]=> string(6) "active" 
                ["etaj"]=> string(2) "50" 
                ["title"]=> string(15) "Test Apartament"
            } 
            [1]=> array(5) { 
                ["date"]=> string(19) "2014-02-04" 
                ["sponsored"]=> string(1) "0" 
                ["offer_status"]=> string(6) "active" 
                ["etaj"]=> string(2) "50"  
                ["title"]=> string(13) "dfasfsdffasdf" 
            } 
    } 
} 

I know it looks wrong but I want that the result should look like this:

["apartamente"][0][date]=>2014-02-05.....[title]
["apartamente"][1][date]=>2014-02-04.....[title]
["garsoniere"][0][date]=>2014-02-04.....[title]
["garsoniere"][1][date]=>2013-03-25.....[title]

Can anyone help me with this ?

kpmDev
  • 1,330
  • 1
  • 10
  • 28
Attila Naghi
  • 2,535
  • 6
  • 37
  • 59

3 Answers3

0

Please use usort function to achieve this. Please have a look at http://badarwaqas.blogspot.com/2013/04/php-sorting-array-on-nth-level-key.html article

0

Use usort

Inital array

Array
(
    [giraffe] => Array
        (
            [date] => 2014-02-04
        )

    [zebra] => Array
        (
            [date] => 2012-02-04
        )

    [penguin] => Array
        (
            [date] => 2014-01-04
        )

)

The sorting algorithm

usort($arr, function($a, $b) {
    return ($a['date'] > $b['date'] ? 1 : ($a['date'] < $b['date'] ? -1 : 0));
});

Which gives - after sorting

Array
(
    [0] => Array
        (
            [date] => 2012-02-04
        )

    [1] => Array
        (
            [date] => 2014-01-04
        )

    [2] => Array
        (
            [date] => 2014-02-04
        )

)

Code grabbed from here

Community
  • 1
  • 1
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
0

Try

$result = array();
foreach($arr as $key=>$val){
   foreach($val as $key1=>$val1){
        $result[$key][$key1]= $val1;
   }
}
ksort($result,SORT_NATURAL);

I am not sure about the sort you have specified in your question. See demo here

Nouphal.M
  • 6,304
  • 1
  • 17
  • 28