0

I have the following array:

    Array
(
    [Sir Ruane] => Array
        (
            [2018-07-03] => Array
                (
                    [Product 1] => Array
                        (
                            [Paid] => Array
                                (
                                    [Ticket] => Array
                                        (
                                            [Standard] => Array
                                                (
                                                    [quantity] => 6
                                                    [asistance] => 6
                                                )

                                        )

                                )

                        )

                )

            [2018-07-01] => Array
                (
                    [Product 1] => Array
                        (
                            [Paid] => Array
                                (
                                    [Ticket] => Array
                                        (
                                            [Standard] => Array
                                                (
                                                    [quantity] => 2
                                                    [asistance] => 2
                                                )

                                        )

                                )

                        )

                )

            [2018-07-06] => Array
                (
                    [Product 1] => Array
                        (
                            [Paid] => Array
                                (
                                    [Ticket] => Array
                                        (
                                            [Standard] => Array
                                                (
                                                    [quantity] => 5
                                                    [asistance] => 5
                                                )

                                        )

                                )

                        )

                )

        )
)

I'm triying to order the array by the key with a date format as they're are printed in date order ascending.

Like this:

Customer name -> Date (ordered descending) -> Product -> Status -> etc.

I tried the following because I've read it in another post in the Stackoverflow community:

 array_multisort( array_column($array, $array[0][0]), SORT_ASC, $array );

But this gave me the array that I wrote first.

I also tried other functions like ksort(), sort(), usort() but nothing does what I'm trying to achieve.

I've also read that sort() function can have a custom function to order things but all examples I've read are column name based and I don't have a column name as keys are always variable depending on user name, date, product name, status, ticket type and ticket name.

Can anyone help me to do this?

  • The function with a custom compare function is called [uksort](http://php.net/manual/en/function.uksort.php). – solarc Aug 08 '18 at 17:48

1 Answers1

0

This may be what you are looking for:

$array=
 Array
(
    "Sir Ruane" => Array
        (
            "2018-07-03" => Array
                (
                    "Product 1" => Array
                        (
                            "Paid" => Array
                                (
                                    "Ticket" => Array
                                        (
                                            "Standard" => Array
                                                (
                                                    "quantity" => 6,
                                                    "asistance" => 6
                                                )

                                        )

                                )

                        )

                ),

            "2018-07-01" => Array
                (
                    "Product 1" => Array
                        (
                            "Paid" => Array
                                (
                                    "Ticket" => Array
                                        (
                                            "Standard" => Array
                                                (
                                                    "quantity" => 2,
                                                    "asistance" => 2
                                                )

                                        )

                                )

                        )

                ),

            "2018-07-06" => Array
                (
                    "Product 1" => Array
                        (
                            "Paid" => Array
                                (
                                    "Ticket" => Array
                                        (
                                            "Standard" => Array
                                                (
                                                    "quantity" => 5,
                                                    "asistance" => 5
                                                )

                                        )

                                )

                        )

                )

        )
);
reset($array);
list($k,$v)=each($array);
uksort($array[$k],function ($a,$b){
    list($a_year,$a_month,$a_day)=explode('-',$a);
    list($b_year,$b_month,$b_day)=explode('-',$b);
    if($a_year>$b_year) return -1;
    if($a_year<$b_year) return 1;
    if($a_month>$b_month) return -1;
    if($a_month<$b_month) return 1;
    if($a_day>$b_day) return -1;
    if($a_day<$b_day) return 1;
    return 0;
});
print_r($array);

and the output is:

Array
(
    [Sir Ruane] => Array
        (
            [2018-07-06] => Array
                (
                    [Product 1] => Array
                        (
                            [Paid] => Array
                                (
                                    [Ticket] => Array
                                        (
                                            [Standard] => Array
                                                (
                                                    [quantity] => 5
                                                    [asistance] => 5
                                                )

                                        )

                                )

                        )

                )

            [2018-07-03] => Array
                (
                    [Product 1] => Array
                        (
                            [Paid] => Array
                                (
                                    [Ticket] => Array
                                        (
                                            [Standard] => Array
                                                (
                                                    [quantity] => 6
                                                    [asistance] => 6
                                                )

                                        )

                                )

                        )

                )

            [2018-07-01] => Array
                (
                    [Product 1] => Array
                        (
                            [Paid] => Array
                                (
                                    [Ticket] => Array
                                        (
                                            [Standard] => Array
                                                (
                                                    [quantity] => 2
                                                    [asistance] => 2
                                                )

                                        )

                                )

                        )

                )

        )

)

Of course if you have more customers you can put the code in a loop this way:

reset($array);
while(list($k,$v)=each($array)){
uksort($array[$k],function ($a,$b){
    list($a_year,$a_month,$a_day)=explode('-',$a);
    list($b_year,$b_month,$b_day)=explode('-',$b);
    if($a_year>$b_year) return -1;
    if($a_year<$b_year) return 1;
    if($a_month>$b_month) return -1;
    if($a_month<$b_month) return 1;
    if($a_day>$b_day) return -1;
    if($a_day<$b_day) return 1;
    return 0;
});
}
Elementary
  • 1,443
  • 1
  • 7
  • 17