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?
$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>";
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.
<?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.
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
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?!