arrays - Randomly select item from JSON in PHP -
i have json string :
[{"format":"i25","content":"172284201241"}, {"format":"i25","content":"40124139"}, {"format":"i25","content":"20197086185689"}, {"format":"i25","content":"10215887"}, {"format":"i25","content":"702666712272"}, {"format":"qrcode","content":"3"}]
and want select 1 of these items randomly,for example :
{"format":"i25","content":"40124139"}
how can php?
that string looks lot json, decode array.
$array = json_decode($string, true);
then, pick random index:
$one_item = $array[rand(0, count($array) - 1)];
and convert json:
$one_item_string = json_encode($one_item); echo $one_item_string;
Comments
Post a Comment