Home >Backend Development >PHP Tutorial >natsort()排列json数据时报错。解决方法
natsort()排列json数据时报错。
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$json = '[{"name":"a1"},{"name":"a10"},{"name":"a12"},{"name":"a3"},{"name":"a5"},{"name":"b21"},{"name":"b2"},{"name":"b11"}]';natsort($json);$data = json_decode($json);foreach ($data as $row) { echo $row->name.'<br>';//}
$json = '[{"name":"a1"},{"name":"a10"},{"name":"a12"},{"name":"a3"},{"name":"a5"},{"name":"b21"},{"name":"b2"},{"name":"b11"}]';$data = json_decode($json);usort($data, 'cmp');function cmp($a, $b) { if ($a->name == $b->name) return 0; return $a->name > $b->name ? 1 : -1;}foreach ($data as $row) { echo $row->name.'<br>';//}<div class="clear"> </div>