I have an array:
$array1 = [
(object)[
'driver_id' => 2,
'total' => 308,
'total_driver' => 5,
'score1' => 61.6000,
'score2' => 62,
],
(object)[
'driver_id' => 3,
'total' => 354,
'total_driver' => 5,
'score1' => 70.8000,
'score2' => 71,
],
];
$array2 = [
[
'driver_id' => 2,
'recommendation_min' => 0,
'recommendation_max' => 50,
'recommendation_key_range' => 'Low',
'recommendation_observation' => 'Low',
'recommendation_' => 'Low',
],
[
'driver_id' => 2,
'recommendation_min' => 51,
'recommendation_max' => 78,
'recommendation_key_range' => 'Moderate',
'recommendation_observation' => 'Moderate',
'recommendation_' => 'Moderate',
],
];
How do I merge array to so that the driver_id 2 object looks like this:
[driver_id] => 2
[recommendation_min] => 51
[recommendation_max] => 78
[recommendation_key_range] => Moderate
[recommendation_observation] => Moderate
[recommendation_] => Moderate
[total] => 308
[total_driver] => 5
[score1] => 61.6000
[score2] => 62
A row should be merged with an object when it has the same driver_id
and the score2
is between recommendation_min
and recommendation_max
.
Basically just need to use array_merge
and a couple of predicate statements to decide which array to merge based on score1
& score2
. Something like this should serve:
$drivers = array();
$drivers[0] = array('driver_id' => 2, 'total' => 308, 'total_driver' => 5, 'score1' => 61.6, 'score2' => 62 );
$drivers[1] = array('driver_id' => 3, 'total' => 354, 'total_driver' => 5, 'score1' => 70.8, 'score2' => 71 );
$recommends = array();
$recommends[0] = array('driver_id' => 2, 'recommendation_min' => 0, 'recommendation_max' => 50, 'recommendation_key_range' => "Low", 'recommendation_observation' => "Low", 'recommendation_' => "Low" );
$recommends[1] = array('driver_id' => 2, 'recommendation_min' => 51, 'recommendation_max' => 78, 'recommendation_key_range' => "Moderate", 'recommendation_observation' => "Moderate", 'recommendation_' => "Moderate" );
$results = array();
foreach($drivers as $driver) {
if($driver['score1'] > 50 || $driver['score2'] > 50) {
$driver = array_merge($driver,$recommends[1]);
} else {
$driver = array_merge($driver, $recommends[0]);
}
$results[] = $driver;
}