0

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?

1
  • Why don't you want to insert elements into $merge array on every iteration? Commented Aug 28, 2013 at 10:43

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

It's ok, but as for each loop array('1','2','3','4') contains different elements
You just replace array('1','2','3','4') with whatever the array is that you have on that loop. if your stuff is in $thisarray then just use $thisarray instead of array('1','2','3','4')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.