Little Pokis
Little Pokis

Reputation: 3

How to Combine two array rand from two array data?

I m so confusing here with this problem, please help me to solve this.

I have 2 array data,

$one = array ("sinta","jojo","wawan","silvie");
$two = array ("eat","sleep","breakfast","sport");

I want to create a random output array from this 2 array, and i want this only pick 2 random array from each array data, so maybe the result would be like this :

$three = array ("sinta","silvie","breakfast","eat");

or

$three = array("jojo","silvie","eat","sleep"); 

and etc..

Upvotes: 0

Views: 50

Answers (2)

jh1711
jh1711

Reputation: 2328

Normally I don't like code only answers, but:

$three = array_merge(array_rand($one, 2), array_rand($two, 2));
shuffle($three);

You can read up on array_rand and array_merge and shuffle in the linked manuals. This code picks 2 elements each from $one and $two at random, and then randomizes the order of the result.

Small warning: If you have string keys in your arrays they will be destroyed.

Upvotes: 1

ratmalwer
ratmalwer

Reputation: 735

$three[0] = $one[rand(0,3)];
$three[1] = $one[rand(0,3)];
$three[2] = $two[rand(0,3)];
$three[3] = $two[rand(0,3)];

thats the logic - I did not php for a while so check the syntax, please. Now you have to assure that rand is not picking the same value twice or youhave to eat two breakfasts :-)

Upvotes: 0

Related Questions