phparraysmultidimensional-array

Reindex keys of deep subarrays in a multidimensional array


I've this array, that I want to re-index the "Mer" elements.

Array

    (
        [Tennis 1] => Array
            (
                [IntervalTime] => Array
                    (
                        [Mer] => Array
                            (
                                [3] => 09:00
                                [4] => 10:00
                                [5] => 11:00
                                [6] => 12:00
                                [7] => 13:00
                                [8] => 14:00
                                [9] => 15:00
                                [10] => 16:00
                                [11] => 17:00
                                [12] => 18:00
                            )
    
                    )
    
            )
    
        [Padel 1] => Array
            (
                [IntervalTime] => Array
                    (
                        [Mer] => Array
                            (
                                [0] => 05:00
                                [1] => 06:00
                                [2] => 07:00
                                [3] => 08:00
                                [4] => 09:00
                                [5] => 10:00
                                [6] => 11:00
                            )
    
                    )
    
            )
    
    )
    
    Array
    (
        [Tennis 1] => Array
            (
                [IntervalTime] => Array
                    (
                        [Mer] => Array
                            (
                                [3] => 09:00
                                [4] => 10:00
                                [5] => 11:00
                                [6] => 12:00
                                [7] => 13:00
                                [8] => 14:00
                                [9] => 15:00
                                [10] => 16:00
                                [11] => 17:00
                                [12] => 18:00
                            )
    
                    )
    
            )
    
        [Padel 1] => Array
            (
                [IntervalTime] => Array
                    (
                        [Mer] => Array
                            (
                                [4] => 09:00
                                [5] => 10:00
                                [6] => 11:00
                            )
    
                    )
    
            )
    
    )

This array is back from

unset($DisponibilitaRoom[$room]["IntervalTime"][$Giorni[$DayOfWeekGiornoScelto]][$index]);

so I need to reindex this:

$DisponibilitaRoom[$room]["IntervalTime"][$Giorni[$DayOfWeekGiornoScelto]]

but something was wrong in my concept.

I've tried array_values() for this multidimensional array but that was not good.


Solution

  • You are correct in wanting to use array_values to reorder indexes. Based on your array structure, this should work:

    $DisponibilitaRoom[$room]["IntervalTime"][$Giorni[$DayOfWeekGiornoScelto]] = array_values($DisponibilitaRoom[$room]["IntervalTime"][$Giorni[$DayOfWeekGiornoScelto]]);