morandmr
morandmr

Reputation: 95

PHP : changes when sorting my multi-dimensional array aren't registered

I'd like to sort an array of arrays basing on an element of these lasts ('clock' field, type=string). My array has the following model :

Fusion
|
| Array_1
| |
| | Array_1_1
| | | clock => "08:08"
| | | //Other fields
| | |
| |
| | Array_1_2
| | | clock => "04:51"
| | | //Other fields
| | | 
|
| Array_2
| ...

I tried it using the usort function, or with a selection sort. Here's my actual code (selection sort) :

foreach ($fusion as $fus){
        for($i = 0 ; $i < count($fus) ; $i++){
            $min = $i;
            for($j = $i+1 ; $j < count($fus) ; $j++){
                //conversion from string to date to compare properly
                $date1 = date_create_from_format('H:i',$fus[$j]['clock']);
                $date2 = date_create_from_format('H:i',$fus[$min]['clock']);
                if($date1 < $date2){
                    $min = $j;
                }
            }
            if($min != $i){
                $tmp = $fus[$i];
                $fus[$i] = $fus[$min];
                $fus[$min] = $tmp;
            }
        }
    }

When I run my code, I can see that changes in the Arrays_X take place well, but when I print the entire array, sorting changes are no longer present, e.g. :

Before sorting :

Array ( 
[0] => Array ( [clock] => 16:49 //other fields ) 
[1] => Array ( [clock] => 00:04 ... ) 
[2] => Array ( [clock] => 22:01 ... ) )

After sorting (OK):

Array ( 
[0] => Array ( [clock] => 00:04 ... ) 
[1] => Array ( [clock] => 16:49 ... ) 
[2] => Array ( [clock] => 22:01 ... ) )

With the entire array (KO) :

[...]
Array ( 
[0] => Array ( [clock] => 16:49 ... ) 
[1] => Array ( [clock] => 00:04 ... ) 
[2] => Array ( [clock] => 22:01 ... ) )
[...]

As you can see, changes are no longer present.

Do you have any idea ? I don't understand why changes aren't registered... Thanks for your help.

Upvotes: 0

Views: 72

Answers (1)

Progrock
Progrock

Reputation: 7485

With usort on each element:

<?php
$fusion = array(
    array(
        array(
        'clock' => '08:08'
        ),
        array(
        'clock' => '04:51'
        )
    ),
    array(
        array(
        'clock' => '11:47'
        ),
        array(
        'clock' => '04:23'
        )
    ),
);

foreach($fusion as &$item) {
    usort(
        $item, 
        function($a, $b)
        {
            $dateA =  date_create_from_format('H:i', $a['clock']);
            $dateB =  date_create_from_format('H:i', $b['clock']);

            return ($dateA > $dateB);
        }
    );
}

var_dump($fusion);

Output:

array (size=2)
  0 => 
    array (size=2)
      0 => 
        array (size=1)
          'clock' => string '04:51' (length=5)
      1 => 
        array (size=1)
          'clock' => string '08:08' (length=5)
  1 => &
    array (size=2)
      0 => 
        array (size=1)
          'clock' => string '04:23' (length=5)
      1 => 
        array (size=1)
          'clock' => string '11:47' (length=5)

Upvotes: 1

Related Questions