Inserting elements from one array into random locations within another array?

Given two arrays, how can one insert all the elements from one array into random locations within the other, while keeping the the items from both original arrays in order relative to each other?

For given, given

``````\$arr_a = array('1','2','3','4','5','6');
\$arr_b = array('apples','organges','bananas','peaches');
``````

The result would be something like:
1, apples, oranges, 2, 3, bananas, 4, 5, 6, peaches

Where 2 comes after 1, and oranges comes after apples, etc.

I am not 100% sure what you mean, but would the following do?

``````\$arr_a = array('1','2','3','4','5','6');
\$arr_b = array('apples','organges','bananas','peaches');

\$merged = array_merge(\$arr_a, \$arr_b);
shuffle(\$merged);

echo "<pre>".print_r(\$merged, true)."</pre>";
``````

Which would output something as follows:

``````Array
(
[0] => 3
[1] => bananas
[2] => 2
[3] => apples
[4] => 4
[5] => 1
[6] => peaches
[7] => organges
[8] => 5
[9] => 6
)

``````

No, I don’t want them shuffled. I’d like the orders of the elements to stay as they are, as in the example I gave.

Ok then.

Try something like this:

``````\$arr_a = array('1','2','3','4','5','6');
\$arr_b = array('apples','oranges','bananas','peaches');

// How many elements are in the array we are inserting into
\$count = count(\$arr_a);

// What was the last insert position? Make sure the next will be greater than this
\$prev = 0;

// Loop through each of our elements to insert
foreach(\$arr_b as \$value)
{
// Generate a random value, higher than the previous
// random number but still less than count(\$arr_a)
\$rand = rand(\$prev, \$count);
// Store this as the previous value + 1
\$prev = \$rand + 1;
// Insert our value into \$arr_a at the random position
array_splice(\$arr_a, \$rand, 0, \$value);
}

echo "<pre>".print_r(\$arr_a, true)."</pre>";
``````

Which can output something similar to:

``````Array
(
[0] => 1
[1] => apples
[2] => oranges
[3] => 2
[4] => bananas
[5] => 3
[6] => peaches
[7] => 4
[8] => 5
[9] => 6
)

``````

Thank you

There does seem to be one minor bug though, where the last inserted element might be out of order, for instance:

``````Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => apples
[7] => oranges
[8] => peaches
[9] => bananas
)
``````

The issue seems to be that on each loop iteration \$rand/\$prev go from 6/7, to 7/8, to 8/9, and then stays at 8/9. I wasn’t able to figure out how to fix this though.

perhaps try this

``````
<?php
\$arr_a = array('1', '2', '3', '4', '5', '6');
\$arr_b = array('apples', 'oranges', 'bananas', 'peaches');

// What was the last insert position? Make sure the next will be greater than this
\$prev = 0;

// Loop through each of our elements to insert
foreach (\$arr_b as \$value) {
// Generate a random value, higher than the previous
// random number but still less than count(\$arr_a)
\$rand = rand(\$prev, count(\$arr_a));
// Store this as the previous value + 1
\$prev = ++\$rand;
// Insert our value into \$arr_a at the random position
array_splice(\$arr_a, \$rand, 0, \$value);
}

echo "<pre>" . print_r(\$arr_a, true) . "</pre>";

?>

``````

I am not completely sure there is a 100% sure there is a robust solution to this.

However you could make it so on the first interation it makes sure the random number is lower than the count(\$arr_a) - count(\$arr_b) + 1 (just plus 1 to give the others a bit more room maybe?)

So in total:

``````\$arr_a = array('1','2','3','4','5','6');
\$arr_b = array('apples','oranges','bananas','peaches');

// How many elements are in the array we are inserting into
\$count = count(\$arr_a);

// What was the last insert position? Make sure the next will be greater than this
\$prev = 0;

// Loop through each of our elements to insert
foreach(\$arr_b as \$value)
{
// Make sure first random position is no higher than the total count
// minus the count of \$arr_b + 1
if(\$prev === 0)
{
\$rand = rand(\$prev, \$count - (count(\$arr_b) + 1));
}
else
{
// Generate a random value, higher than the previous
// random number but still less than count(\$arr_a)
\$rand = rand(\$prev, \$count);
}
// Store this as the previous value + 1
\$prev = ++\$rand ;

// Insert our value into \$arr_a at the random position
array_splice(\$arr_a, \$rand, 0, \$value);
}

echo "<pre>".print_r(\$arr_a, true)."</pre>";
``````

Ideally the \$arr_a should be a LOT longer than \$arr_b to give elements from \$arr_b a higher chance of random insertion. Unless there could be some formula to work out the initial number and make sure the next insert is no higher than a certain range away from the last insert (therefore you wouldn’t have problems with large jumps like: 1st random = 3, 2nd = 7, 3rd = 8 etc etc). However wouldn’t this then take away some of the “randomness”?

Sorry if you don’t understand what I am trying to get across in the above paragraph, it’s quite hard to explain.

Can I ask why you need it in this certain format and cannot use the inital post i made?

The op said that the elements in \$arr_b are not always kept in their original order when inserted into the other array. The op gave a sample listing where the bug occured.

I got the same bug when I tested your code after about the 20th iteration.

I think (fingers crossed) I fixed the bug in the code I posted. After about 50 iterations the elements in \$arr_b were still in their original order after being inserted.

I haven’t tested any further.

Ah sry Kalon, I did test your code however I ended up with all elements from \$arr_b in \$arr_a right at the end of the array. Not quite sure what I must of done wrong there.

I was merely asking as to “why” it needed to be done in this fashion. I was trying to think of a situation and couldn’t. Therefore i was curious

no problem

basically all I did with your original code was remove the fixed value of \$count and recalculate it after each new element is added to \$arr_a.

after 50 iterations all the inserted elements were then still in their correct order, so hopefully that got rid of the bug.

Maybe it’s late here but regarding the OP’s post there is nothing random he is asking for. Tribal_01, your first solution actually would do it since it randomizes everything. However asking to have the 2nd array’s
value stay relative to each other… it’s kind of awkward since in the first place, you are “moving” them by adding new values… a new paradox?!

Oh DUH ! I completely overlooked that Kalon. Lol thanks.

Thanks for the help guys