Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Combine two arrays randomly in PHP

Tags:

arrays

php

random

How to combine two arrays into single one and i am requesting this in such a way that the 3rd combination array should contains one value from one array and the next one from other array and so on.. or ( it could be random) ex:

$arr1 = (1, 2, 3, 4, 5);
$arr2 = (10, 20, 30, 40, 50);

and combined array

$arr3 = (1, 10, 2, 20, 3, 30, ...);
like image 225
KillerFish Avatar asked Dec 07 '10 11:12

KillerFish


1 Answers

If it can be random, this will solve your problem:

$merged = array_merge($arr1, $arr2);
shuffle($merged);
like image 94
kapa Avatar answered Oct 05 '22 16:10

kapa