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

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -