I'v written 2 codes to shuffle some arrays which are in a for loop
First:
$numbers = range(0, 5);
shuffle($numbers);
foreach ($numbers as $number) {
$arr = array("user" => array("id" => $array_id[$number], "name" => $array_name[$number], "key" => $array_key[$number]));
}
echo json_encode($arr);
This one has a big problem and that's if the $number
in one of the arrays equals to 5 it won't loop to put another result of $number
in the that array, for example the result wold be something like this:
{"user":{"id":["18","2","36"],"name":["alex","john"],"key":["159","228,"486,"852"]]}}
All of my arrays have 5 values in them and you can see it returned them defectively.I'll be thankful if anyone can tell me why when $number
gets the max value in range by shuffle and array gets that $number
it stocks?
Second:
function shuffle_assoc(&$array) {
if (shuffle($array)) {
return $array;
}else{
return FALSE;
}
}
for ($y=0; $y<sizeof($array_id); $y++) {
$arr = array("user" => array("id" => shuffle_assoc($array_id), "name" => shuffle_assoc($array_name), "key" => shuffle_assoc($array_key)));
}
echo json_encode($arr);
This one works fine But I want the id
and name
to be match I mean they shuffle the same that's why I wrote the first code.anyway for this one if anyone knows how to make id
and name
shuffle the same I'll be appreciate that.(sorry if I had mistakes in my writing my first language isn't English but I love English :)
Aucun commentaire:
Enregistrer un commentaire