How to insert random array entries into another array

Let it say that 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 $numbers keep order. The goal is to have an array like this:

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

How can i achieve this?

+4
source share
1 answer
 foreach($letters as $letter) { array_splice($numbers, rand(0, count($numbers)), 0, $letter); } print_r($numbers); 
+10
source

Source: https://habr.com/ru/post/1399491/


All Articles