I have this object thrown by an API and I would like to recreate it into a new array and combine the time value of each element that has the same date value
Array (
[0] => Array (
[id] => 6
[time] => morning
[date] => 2015-09-29
)
[1] => Array (
[id] => 5
[time] => night
[date] => 2015-09-29
)
[2] => Array
[id] => 31
[time] => morning
[date] => 2015-12-07
)
[3] => Array
[id] => 3
[time] => night
[date] => 2015-11-15
)
)
I have this php code, but its not working properly, second element with the same date won't add the time value on existing key and I'm not sure how to properly set the $full variable
$date = array();
foreach( $datas as $data) {
$full = count( $data['time'] ) > 1 ? true : false;
$dates[$data['date']] = array(
'date' => $data['date'],
'fully_book' => $full,
'time' => array( $data['time'] ),
);
}
The result I really wanted to get is something like this,
array(
[2015-09-29] => array(
[date] => 2015-09-29
[fully_book] => true,
[time] => array(
[0] => morning,
[1] => night
)
),
[2015-12-07] = array(
[date] => 2015-09-29,
[fully_book] => false,
[time] => array(
[0] => night
)
),
[2015-11-15] = array(
[date] => 2015-11-15,
[fully_book] => false,
[time] => array(
[0] => morning
)
)
Would appreciate any help,
Thanks
$newdata = array( 'time' => $data['time'], ); if ( array_key_exists($data['date'], $date) ) { array_push($data['date'], $newdata); }but it just messing up, I don't even know if array_key_exists and array_push be an optiontimevalues always bemorning/night, and only one of each per date?