Consider the following array $arr_sessions:
[
{
"url": "2019_7_5_0_52_RACE.json",
"trackName": "road-america"
},
{
"url": "2019_7_5_0_27_RACE.json",
"trackName": "road-america"
},
{
"url": "2019_7_5_0_16_QUALIFY.json",
"trackName": "road-america"
},
{
"url": "2019_7_5_0_0_PRACTICE.json",
"trackName": "road-america"
},
{
"url": "2019_7_4_23_9_RACE.json",
"trackName": "ttassen"
},
{
"url": "2019_7_4_23_52_RACE.json",
"trackName": "ttassen"
},
{
"url": "2019_7_2_11_25_RACE.json",
"trackName": "road-america"
},
{
"url": "2019_7_2_11_03_RACE.json",
"trackName": "road-america"
}
]
Based upon this array I'd like to create a new $arr_results as follows:
[
{
"url": [
"2019_7_5_0_52_RACE.json",
"2019_7_5_0_27_RACE.json",
"2019_7_5_0_16_QUALIFY.json",
"2019_7_5_0_0_PRACTICE.json"
],
"trackName": "road-america"
},
{
"url": [
"2019_7_4_23_9_RACE.json",
"2019_7_4_23_52_RACE.json"
],
"trackName": "ttassen"
},
{
"url": [
"2019_7_2_11_25_RACE.json",
"2019_7_2_11_03_RACE.json"
],
"trackName": "road-america"
}
]
As you can see, the requirements here are to combine the values for 'url' for as long as 'trackName' stays the same as the next one, however don't group them if the same 'trackName' should exist somewhere else (like in this example we have 2 times "road-america", however these shouldn't get combined since there is a different 'trackName' in between).
So far I spend significant time trying to figure it out how to get there (I guess this has to be done with nested loops?)
Update
Here's 1 of the things I tried (but clearly this only looks at the next 'trackName' - not sure how to continue from here)
for ($s = 0; $s < count($arr_sessions); $s++) {
$n= $s+1;
if($arr_sessions[$n]["trackName"] == $arr_sessions[$s]["trackName"]){
$arr_results[$s]['name'] = $arr_sessions[$s]["sessionName"];
$arr_results[$s]['log'] = array($arr_sessions[$s]["url"], $arr_sessions[$n]["url"]);
$s += 1;
}
}
trackName. Then you check if$current['trackName'] == $prev_trackname. If it is, you add to the current element of the result, otherwise you start a new element.