Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert randomly entries of an array in another array

Tags:

arrays

php

random

Let's says I have this array :

$numbers = array(1,2,3,4,5);

And this array :

$letters = array('A','B','C');

I want to put $letters entries inside $numbers randomly. I don't care about the order of $letters, but I want $numbersto keep the order. The goal is to have this kind of array :

$randomLettersInNumbers = array(1, 'B', 2, 3, 'A', 4, 'C', 5);

How can I achieve this ?

like image 525
lepix Avatar asked Mar 03 '12 00:03

lepix


1 Answers

foreach($letters as $letter)
{
    array_splice($numbers, rand(0, count($numbers)), 0, $letter);
}
print_r($numbers);
like image 97
Aaron W. Avatar answered Oct 19 '22 17:10

Aaron W.