PHP: Merge 2 Multidimensional Arrays

how to join two multidimensional arrays in php

Use the array_merge function, like this:

$C = array_merge($A, $B);
print_r($C);

When I run the above script it'll output:

Array ( 
[0] => Array (
[a1] => 1
[b1] => 2
[c1] => A
)
[1] => Array (
[a1] => 1
[b1] => 16
[c1] => Z
)
[2] => Array (
[a1] => 3
[b1] => 8
[c1] => A
)
[3] => Array (
[a2] => 1
[b2] => A
)
[4] => Array (
[a2] => 1
[b2] => G
)
[5] => Array (
[a2] => 3
[b2] => A
)
)

Take a quick read here: http://php.net/manual/function.array-merge.php

PHP: Merge 2 Multidimensional Arrays

$i=0;
$NewArray = array();
foreach($OriginalArray as $value) {
$NewArray[] = array_merge($value,array($_FILES['Upload']['name'][$i]));
$i++;
}

the [] will append it to the array instead of overwriting.

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'];
}
}
}

php how do i merge 2 multidimensional arrays?

You want to combine not merge. To get a new array:

$result = array_map(function($v) use($columns) {
return array_combine($columns, $v);
}, $tableinfo);

Or simply loop and combine:

foreach($tableinfo as $row) {
$result[] = array_combine($columns, $row);
}

Or to modify $tableinfo:

array_walk(function(&$v) use($columns) {
$v = array_combine($columns, $v);
}, $tableinfo);

Or with the loop:

foreach($tableinfo as &$row) {
$row = array_combine($columns, $row);
}

Merge multidimensional Array PHP without changing the key

You can use Array Operators + to do that. You can check my below demo:

<?php

$array = [
array(
"image" => "logo.jpg",
"name" => "data",
"server"=> "Mirani Rahmawati",
"6" => "5"
),
array(
"image"=> "logo.jpg",
"name" => "data",
"server"=> "Mirani Rahmawati",
"5" => "150"
)
];

$result = [];
foreach ($array as $item) {
$result = $result + $item;
}

print_r($result);
exit;

?>

The result will be:

Array
(
[image] => logo.jpg
[name] => data
[server] => Mirani Rahmawati
[6] => 5
[5] => 150
)

You can find out more at https://www.php.net/manual/en/language.operators.array.php

Merge multiple multidimensional arrays and group by id column

First, why is your code slow? Assuming you have 1000 elements, your code is doing 1000*(1000+1000) = 2 millions iterations. That's why.

How to make it faster? Use assoc arrays instead of normal arrays, i.e. use id_produto as the key, so you can have direct access to the right elements instead of having to do a loop inside a loop.

To do that, first convert array2 and array3. Example with array2:

$new_array2 = array();
foreach ( $array2 as $row ) {
$new_array2[$row['id_produto']] = $row;
}
$array2 = $new_array2;

Then merge:

foreach ( $array1 as & $row ) {
$row['pageviews'] = $array2[$row['id_produto']['pageviews']];
$row['pedidos'] = $array3[$row['id_produto']['pedidos']];
$row['valor'] = $array3[$row['id_produto']['valor']];
}

Total : 3000 iterations only.

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);


Related Topics



Leave a reply



Submit