Created
November 15, 2018 05:17
-
-
Save asahasrabuddhe/17295f7b9f3078f599596c432a04f600 to your computer and use it in GitHub Desktop.
Array Merge Logic for Doctorapp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$total_services = [ | |
0 => [ | |
'id' => 1, | |
'name' => 'a' | |
], | |
1 => [ | |
'id' => 2, | |
'name' => 'b' | |
], | |
2 => [ | |
'id' => 3, | |
'name' => 'c' | |
], | |
3 => [ | |
'id' => 4, | |
'name' => 'd' | |
] | |
]; | |
$doctor_services = [ | |
0 => [ | |
'id' => 2, | |
'name' => 'b', | |
'charges' => 123 | |
], | |
1 => [ | |
'id' => 4, | |
'name' => 'd', | |
'charges' => 456 | |
] | |
]; | |
echo "before\n"; | |
print_r($total_services); | |
print_r($doctor_services); | |
foreach( $total_services as $t_key => $service ) { | |
foreach($doctor_services as $d_key => $d_service) { | |
if($service['id'] == $d_service['id']) { | |
$total_services[$t_key] = array_merge($service, $d_service); | |
} else { | |
if( !array_key_exists('charges', $total_services[$t_key]) ) { | |
$total_services[$t_key]['charges'] = 0; | |
} | |
} | |
} | |
} | |
echo "\nafter\n"; | |
print_r($total_services); | |
print_r($doctor_services); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment