Maison  >  Article  >  développement back-end  >  php小白数组求解 根据某一个键值重新排序数组 给分100

php小白数组求解 根据某一个键值重新排序数组 给分100

WBOY
WBOYoriginal
2016-06-23 13:28:261061parcourir

这样一个json数组
[ {"id":"1","name":"21g","dis":"33"},
  {"id":"2","name":"21g","dis":"22"},
  {"id":"3","name":"21g","dis":"3"},
  {"id":"4","name":"21g","dis":"5"},
  {"id":"5","name":"21g","dis":"12"}]    
需要根据dis从小到大排序成
[{"id":"3","name":"21g","dis":"3"},
 {"id":"4","name":"21g","dis":"5"},
 {"id":"5","name":"21g","dis":"12"},
 {"id":"2","name":"21g","dis":"22"},
 {"id":"1","name":"21g","dis":"33"}]

求步骤详情  在线等      急!!!


回复讨论(解决方案)

$s =<<< TXT[ {"id":"1","name":"21g","dis":"33"},  {"id":"2","name":"21g","dis":"22"},  {"id":"3","name":"21g","dis":"3"},  {"id":"4","name":"21g","dis":"5"},  {"id":"5","name":"21g","dis":"12"}]    TXT;$a = json_decode($s, true);foreach($a as $v) $r[] = $v['dis'];array_multisort($r, $a);echo json_encode($a);
[{"id":"3","name":"21g","dis":"3"},{"id":"4","name":"21g","dis":"5"},{"id":"5","name":"21g","dis":"12"},{"id":"2","name":"21g","dis":"22"},{"id":"1","name":"21g","dis":"33"}]

谢谢谢谢!!  已经弄好了!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn