Merging two multidimensional arrays on specific key
You can just do a nested loop and check if the id
values match, then add title
to $first
(or name
to $second
)
foreach($first as $key => $value){
foreach($second as $value2){
if($value['id'] === $value2['id']){
$first[$key]['title'] = $value2['title'];
}
}
}
Combine Two Multidimensional Arrays on matching key values in PHP
<?php
// Your array
$arr1 = [
[
'course_id' => 10,
'topic' => 'Booda-hood, advice and an important pit.',
'lesson_id' => 10
],
[
'course_id' => 10,
'topic' => 'Booda-hood, advice and an important pit.',
'lesson_id' => 11
]
];
$arr2 = [
[
'id' => 10,
'description' => '10 Lorem Ipsum is blablabla',
'course_title' => '10 Lorem Ipsum is blablabla'
],
[
'id' => 11,
'description' => '11 Lorem Ipsum is blablabla',
'course_title' => '11 Lorem Ipsum is blablabla'
]
];
// Combining arrays implementation
foreach ( $arr2 as $k=>$v )
{
// rename array2 `id` to `lesson_id`
$arr2[$k]['lesson_id'] = $arr2[$k] ['id'];
unset($arr2[$k]['id']);
}
// array_replace_recursive — Replaces elements from passed arrays into the first array recursively
print_r(array_replace_recursive($arr1, $arr2));
?>
OUTPUT
[
{
"course_id": 10,
"topic": "Booda-hood, advice and an important pit.",
"lesson_id": 10,
"description": "10 Lorem Ipsum is blablabla",
"course_title": "10 Lorem Ipsum is blablabla"
},
{
"course_id": 10,
"topic": "Booda-hood, advice and an important pit.",
"lesson_id": 11,
"description": "11 Lorem Ipsum is blablabla",
"course_title": "11 Lorem Ipsum is blablabla"
}
]
Hope this helps!
Ref - array_replace_recursive
Merge two multidimensional arrays with same key pair
I think this can work for you:
$arr_1 = array(333 => array('a1' => 1));
$arr_2 = array(333 => array('a2' => 2), 444 => array('a3' => 3));
$arr_res = array_replace_recursive($arr_1, $arr_2);
var_dump($arr_res);
How to merge two multi dimensional arrays by key value in php?
In this case, I walk the arrays and construct the desired output.
$output = [];
$allMerged = array_merge($arr1, $arr2);
array_walk($allMerged,
function($item, $key) use (&$output) {
print_r($item);
if ( array_key_exists($item['month'], $output) ) {
if ( isset($item['info']) ) {
$output[$item['month']]['info'] = $item['info'];
} else {
$output[$item['month']] = $item;
}
} else {
$output[$item['month']] = $item;
}
});
print_r($output);
How can I merge between two multidimensional array respected their keys
Pretty simple, just loop the second array and merge using the key to the first array:
foreach($array2 as $key => $val) {
$array1[$key]['event_detail'] = array_merge($array1[$key]['event_detail'],
$val['event_detail']);
}
PHP library function to merge multidimensional arrays into one based on key?
I found the answer based on the same question :
$first = array_column($data['all_client'], null, 'client_id');
$second = array_column($data['all_client_document'], null, 'client_id');
$result = array_values(array_replace_recursive($first, $second));
It reproduces as what I want also and simpler, thanks anyone!
Related Topics
MySQL Error: 1364 Field 'Display_Name' Doesn't Have Default Value
Display Message Before Redirect to Other Page
How Long Can a Tld Possibly Be
PHP Create and Save a Txt File to Root Directory
Differencebetween Sessions and Cookies in PHP
Laravel - Using (:Any) Wildcard for All Routes
Relative Path Not Working in Cron PHP Script
How to Password Protect Folder/Page Using PHP Without a Db or Username
Scaling a Chat App - Short Polling VS. Long Polling (Ajax, PHP)
Laravel 4 - Logging SQL Queries
Encrypt Files Using Pgp in PHP
How to Store an Array in a File to Access as an Array Later with PHP
How to Can Post a Multiple Photos via Facebook API
PHP String Function to Get Substring Before the Last Occurrence of a Character