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
PHP: Sort an Array by the Length of Its Values
Append Data to a .Json File With PHP
Converting MySQL Result Array to Json
Convert Latin1 Characters on a Utf8 Table into Utf8
A Formula to Find Prime Numbers in a Loop
PHP Fatal Error: Call to Undefined Function Json_Decode()
How to Fix the Permission Error When I Call Session_Start()
Passing an Array as Arguments, Not an Array, in PHP
The Accuracy of PHP Float Calculate
Rebase Array Keys After Unsetting Elements
How to Implement a Callback in PHP
Create Ini File, Write Values in PHP
Sha1 VS Md5 VS Sha256: Which to Use For a PHP Login
How to Create Multilingual Translated Routes in Laravel
Workaround For Basic Syntax Not Being Parsed