An error is reported when natsort () arranges json data. PHPcode $ json = '[{& quot; name & quot;: & quot; a1 & quot ;},{ & quot; name & quot;: & quot; a10 & quot ;},{ & quot; name & quot ;:& quo natsort () an error is returned when arranging json data.
PHP code
$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.'
';//}
Why is the prompt "Warning: natsort () expects parameter 1 to be array, string given in d: \ www \ test. php" displayed?
What is the difference between natsort () and strnatcmp?
Thank you.
------ Solution --------------------
Natsort is an array sorting function and cannot be used for character seek.
------ Solution --------------------
PHP code
$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.'
';//}