PHP - How to Merge Arrays Inside Array

PHP - How to merge arrays inside array

array_merge can take variable number of arguments, so with a little call_user_func_array trickery you can pass your $result array to it:

$merged = call_user_func_array('array_merge', $result);

This basically run like if you would have typed:

$merged = array_merge($result[0], $result[1], .... $result[n]);

Update:

Now with 5.6, we have the ... operator to unpack arrays to arguments, so you can:

$merged = array_merge(...$result);

And have the same results. *

* The same results as long you have integer keys in the unpacked array, otherwise you'll get an E_RECOVERABLE_ERROR : type 4096 -- Cannot unpack array with string keys error.

Merge array inside array

If what you want is this:

array(
array('id'=>'1', 'name'=>'Mike'),
array('id'=>'2', 'name'=>'Lina'),
array('id'=>'3', 'name'=>'Niken')
)

You can just add the second as a new element to the first:

$one[] = $two;

PHP: How can I merge array values with array values of dynamic amount of arrays?

Use the following recursive function for the cartesian product of a variable number of arrays:

function array_cartesian_product(...$array)
{
if (empty($array)) return [[]];

$column = array_shift($array);
$cartesian = array_cartesian_product(...$array);

$result = [];
foreach ($column as $item) {
foreach ($cartesian as $row) {
array_unshift($row, $item);
array_push($result, $row);
}
}
return $result;
}

// Usage:

$a1 = ['a', 'b'];
$a2 = ['c', 'd', 'e', 'f'];
$a3 = ['g', 'h'];

$result = array_cartesian_product($a1, $a2, $a3);

print_r($result);

$a = [ ['a', 'b'], ['c', 'd', 'e', 'f'], ['g', 'h'] ];

$result = array_cartesian_product(...$a);

print_r($result);

Merge arrays inside array dynamically which have the same keys

This is not exactly as the result you asked for and it feels pretty messy, but it might be something you can work further with :)

$result = [];

foreach ($array as $answers) {
foreach ($answers as $email => $values) {
if (!isset($result[$email])) {
$result[$email] = $values;
continue;
}
$result[$email] = array_merge($result[$email], $values);
}
}

And the result:

$result === [
'johndrake@gmail.com' => [
'email' => 'johndrake@gmail.com',
'firstname' => 'john',
'lastname' => 'drake',
'food' => 'burger',
'drink' => 'coke',
],
'samwin@gmail.com' => [
'email' => 'samwin@gmail.com',
'firstname' => 'sam',
'lastname' => 'win',
'food' => 'pizza',
'drink' => 'pepsi',
],
]

Laravel merge arrays inside an array


Solved

the solution was using ... operator to merge my inside arrays

array_merge(...$barcodes) and that did the job.

How to merge arrays in one variable in PHP

Use array_merge with ... splat operator

 $result = array_merge(...$array);

OR

 $result = call_user_func_array('array_merge', array($a));

For example :- https://3v4l.org/38Q5h

PHP Array of Objects with arrays inside. Merge these

Your oneliner is:

print_r(array_merge(...array_column($array, 'datas')));

Working fiddle.

Merge values from multiple arrays in PHP

Use array_merge() to combine arrays, not array_push().

Remove the duplicates at the end after merging everything.

$json = $this->curl_get_marketplace_contents();
$data = json_decode($json, true);
$categories = array();
foreach ($data['themes'] as $theme) {
$array = explode(",", $theme['categories']);
$array = array_map('trim', $array);
$categories = array_merge($array, $categories);

};
return array_unique($categories);


Related Topics



Leave a reply



Submit