I'm not very good with PHP arrays/key stuff and I've been struggling with this one for the 3 past hours so I'm looking for help. I need a new look on this matter. I've done some research but all I found was only for 2 keys so I got confused.
I have this array :
Array ( [0] => Array ( [season] => 1 [start] => 2013 [end] => 2014 [championship] => Array ( [0] => Array ( [name] => Ligue 1 [result] => 3 )
) ) [1] => Array ( [season] => 3 [start] => 2012 [end] => 2013 [championship] => Array ( [0] => Array ( [name] => Ligue 1 [result] => 1 ) ) ) [2] => Array ( [season] => 3 [start] => 2012 [end] => 2013 [championship] => Array ( [0] => Array ( [name] => Coupe de France [result] => 1 ) ) ) [3] => Array ( [season] => 4 [start] => 2011 [end] => 2012 [championship] => Array ( [0] => Array ( [name] => Ligue 1 [result] => 1 ) ) )
)
As you can see, some values are identical and some are not. What I want to do is to regroup the "championship" key values for the same season ID. So my output will look like this :
Array ( [0] => Array ( [season] => 1 [start] => 2013 [end] => 2014 [championship] => Array ( [0] => Array ( [name] => Ligue 1 [result] => 3 )
) ) [1] => Array ( [season] => 3 [start] => 2012 [end] => 2013 [championship] => Array ( [0] => Array ( [name] => Ligue 1 [result] => 1 ) [1] => Array ( [name] => Coupe de France [result] => 1 ) ) ) [2] => Array ( [season] => 4 [start] => 2011 [end] => 2012 [championship] => Array ( [0] => Array ( [name] => Ligue 1 [result] => 1 ) ) )
)
Any help would be appreciated.
Thanks in advance.
#EDIT EDIT EDIT
#Took some air and finally got back with a solution
function sortList($arrayToSort) {
$array = $arrayToSort;
for ($i = 0; $i < count($array); $i++) {
for ($j = 0; $j < count($array); $j++) {
if ($array[$i]['season'] == $array[$j]['season'] && $i != $j) {
$array[$i]['championship'] = array_merge($array[$i]['championship'], $array[$j]['championship']);
unset($array[$j]);
}
}
}
return array_values($array);
}
Which seems to work. But I'm not sure this is safe, meaning : no result will be lost or removed if not needed. It seems that PHP doesnt reindex when we use unset() in an array...but if anyone could confirm this is the right thing to do, it will help. What concernes me here is that we modify the array while iterating it, so this solution doesnt sound like a "good practice".