is there any way to merge undefined number of arrays? Array_merge doesn't work for me, cause you have to actually put those arrays as parameters, or maybe there is a way.
I've chunked an array into n - number of arrays, I do some stuff on those chunks and would like to merge some other arrays:
$chunky = array_chunk($positions);
$arraytomerge = array();
foreach($chunky as $key=>$val)
{
do some stuff with $keys and $vals
$arraytomerge[] = array('1','2','3','4');
}
$merged = array_merge($arraytomerge[0],$arraytomerge[1]...);
How to list arrays as array_merge parameters?
Instead of doing
//do some stuff with $keys and $vals
$arraytomerge[] = array('1','2','3','4');
Just do
//do some stuff with $keys and $vals
$merged = array_merge($merged,array('1','2','3','4'));
Or better yet, just add your new items directly to the $merged array instead of creating a new array
Related
I have a multi dimension array that I want to merge all the inside arrays into one singer dimension array, I have tried array_merge with foreach but it doesn't help.
Example Array:
$nums = array (
array(1,2,3),
array(4,5,6),
array(7,8,9)
);
What I did but get an empty array
$newArr = [];
foreach ($nums as $value) {
array_merge($newArr, $value);
}
Expectation
$newArr = array(1,2,3,4,5,6,7,8,9)
You could use the function array_merge() this way :
$newArr = array_merge(...$nums)
It would make your code lighter and avoid the use of a foreach loop.
array_merge returns the results of the merge rather than acting on the passed argument like sort() does. You need to be doing:
$newArr = array_merge($newArr, $value);
I have an array of numeric subarrays. I want to sort all the subarrays, and then sort the whole array, and remove duplicates. Using sort($val) doesn't work, so I found the following workaround with $derp, which I find insanely stupid:
$arr = array( array(5,6), array(1,2), array(6,5) );
foreach ($arr as $key => $val) {
$derp = $val;
sort($derp);
$arr[$key] = $derp;
}
sort($arr);
$arr = array_map("unserialize", array_unique(array_map("serialize", $arr)));
The result is array( array(1,2), array(5,6) ). Is that is the correct way to do this in PHP, or is there a better and shorter way?
I created a pastebin as a response to the first answer: pastebin.com/Y5vNvKKL
This question is not anymore just about a less goofy way to write this: Now the question is:Why does sort() in array_work() not give the same result as sort() in foreach?
By the way: This is about finding the partitions of multisets.
I'd approach it like this:
array_walk($arr, 'sort');
$deduped = array();
foreach ($arr as $val) {
$deduped[serialize($val)] = $val;
}
$arr = array_values($deduped);
Let's say I have a data set in PHP that is in the form of:
$array = [{"prior":"0","id":"61039","type":"todo"},
{"prior":"1","id":"70341","type":"todo"},
{"prior":"3","id":"39104","type":"todo"},
{"prior":"4","id":"70315","type":"todo"},
{"prior":"6","id":"72050","type":"todo"},
{"prior":"7","id":"72329","type":"todo"},
{"prior":"8","id":"73992","type":"todo"}]
I want to process this array of arrays so that I have a single array with integer indexes and the values of only id.
It's trivial to simple use loops:
$data = array();
foreach($array as $item){
$data[] = $item['id'];
}
What I want to know, is there a way to do this, disregarding efficiency, using the built in array functions of PHP (with no loops), or am I stuck using the foreach loop?
You can use array_map() for this:
$data = array_map(function ($el) { return $el['id']; }, $array);
Note that this still has to perform the looping internally; it's just not shown explicitly as in a foreach loop. This approach will be far less efficient than a plain 'ol foreach.
Demo
With PHP >= 5.5.0:
$array = json_decode($array, true);
$ids = array_column($array, 'id');
I have an array that contains names of arrays
$names_array[] = ('$array1', '$array2', $array3'....)
The $names_array[] is dynamically updated so it may contain 2 or more different names.
When the script is executed the values of listed arrays in $names_array[] need to be merged.
I think in case of merging it is not a problem
u can merge $result = array_merge($array1, $array2);
http://php.net/manual/en/function.array-merge.php
I think it can be done with variable variables.
$arraymerge = array();
foreach ($names_array as $arrayname)
{
$arraymerge = array_merge($arraymerge, ${$arrayname});
}
Thanks for help... I have went around the problem: if anyone needs to merge dynamically generated arrays, in my case I have six arrays that exist or not, so I need to merge the existing ones. What I did is:
if(!is_array($array1[$i])) $array1[$i]=array();
if(!is_array($array2[$i])) $array2[$i]=array();
if(!is_array($array3[$i])) $array3[$i]=array();
if(!is_array($array4[$i])) $array4[$i]=array();
if(!is_array($array5[$i])) $array5[$i]=array();
if(!is_array($array6[$i])) $array5[$i]=array();
$combineddata[$i]=array_merge($array1[$i], $array2[$i],$array3[$i],$array4[$i], $array5[$i], $array6[$i]);
In case 'array_x[$i]' doesn't exists array_merge doesn't break the script just merges empty array.
Thanks
$names_array = array ('array1', 'array2', 'array3');
$array1 = array ('a','b','c');
$array2 = array ('d','e','f');
$array3 = array ('g','h','i');
$result = array ();
foreach ($names_array as $x) {
$result = array_merge ($result, $$x);
}
print_r ($result);
I have this kind of an array containing single-element arrays:
$array = [[88868], [88867], [88869], [88870]];
I need to convert this to one dimensional array.
Desired output:
[88868, 88867, 88869, 88870]
Is there any built-in/native PHP functionality for this array conversion?
For your limited use case, this'll do it:
$oneDimensionalArray = array_map('current', $twoDimensionalArray);
This can be more generalized for when the subarrays have many entries to this:
$oneDimensionalArray = call_user_func_array('array_merge', $twoDimensionalArray);
The PHP array_mergeĀDocs function can flatten your array:
$flat = call_user_func_array('array_merge', $array);
In case the original array has a higher depth than 2 levels, the SPL in PHP has a RecursiveArrayIterator you can use to flatten it:
$flat = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), 0);
See as well: Turning multidimensional array into one-dimensional array
try:
$new_array = array();
foreach($big_array as $array)
{
foreach($array as $val)
{
array_push($new_array, $val);
}
}
print_r($new_array);
$oneDim = array();
foreach($twoDim as $i) {
$oneDim[] = $i[0];
}
Yup.
$values = array(array(88868), array(88867), array(88869), array(88870));
foreach ($values as &$value) $value = $value[0];
http://codepad.org/f9KjbCCb
foreach($array as $key => $value){
//check that $value is not empty and an array
if (!empty($value) && is_array($value)) {
foreach ($value as $k => $v) {
//pushing data to new array
$newArray[] = $v;
}
}
}
For a two dimensional array this works as well:
array_merge(...$twoDimensionalArray)
While some of the answers on the page that was previously used to close this page did have answers that suited this question (like array_merge(...$array)). There are techniques for this specific question that do not belong on the other page because of the input data structure.
The sample data structure here is an array of single-element, indexed arrays.
var_export(array_column($array, 0));
Is all that this question requires.
If you ever have a daft job interview that asks you to do it without any function calls, you can use a language construct (foreach()) and use "array destructuring" syntax to push values into a result variable without even writing a body for the loop. (Demo)
$result = [];
foreach ($array as [$result[]]);
var_export($result);
Laravel also has a flattening helper method: Arr::flatten()