I know it’s simple task, but my mind not clicking at present for appropriate solution.
I have 2 arrays
$array1=Array
(
[302] => Array
(
[id] => 302
[medical_id] => 55
[medication_name] => disprin
[medication_frequency] => 1
[medication_status] => Stoped
[users_id] => 106
[update_date] => 2012-03-02 11:03:57
)
)
$array2=Array
(
[302] => Array
(
[medication_id] => 302
[id] => 14
[medical_id] => 55
[medication_name] => disprin
[medication_frequency] => 2
[medication_status] => Stoped
[users_id] => 106
[update_date] => 2012-03-02 11:03:57
[change_date] =>
)
[305] => Array
(
[medication_id] => 305
[id] => 15
[medical_id] => 57
[medication_name] => disprin
[medication_frequency] => 2
[medication_status] => Continued
[users_id] => 106
[update_date] => 2012-03-02 11:02:46
[change_date] =>
)
)
I want Output:
$outputarray=Array
(
[302] => Array
(
[id] => 302
[medical_id] => 55
[medication_name] => disprin
[medication_frequency] => 1
[medication_status] => Stoped
[users_id] => 106
[update_date] => 2012-03-02 11:03:57
)
[305] => Array
(
[medication_id] => 305
[id] => 15
[medical_id] => 57
[medication_name] => disprin
[medication_frequency] => 2
[medication_status] => Continued
[users_id] => 106
[update_date] => 2012-03-02 11:02:46
[change_date] =>
)
)
means if same key is exist in array1 then take it from array1 else from array2.
I tried with some PHP functions like array_merge,array_merge_recursive but not got desired output.
am not able to recollect memory now, Please suggest me any PHP array function to sort out this.
It’s so simple that it’s almost unethical:
Array union (above) does what you describe. Using
array_mergecould also work in general, but not directly with such input because your keys are integers.