use*_*347 5 php multidimensional-array
有人可以帮我一些PHP.
原始代码〜有效,但输出顺序错误.所以我需要反转JSON数组的顺序/顺序.
但是当我尝试使用下面的PHP(提取)代码来反转序列时:
$json = file_get_contents($url,0,null,null);
$tmp = json_decode($json, true); // using a temp variable for testing
$result = array_reverse($tmp); // <--new line to reverse the arrray
foreach ($result['data'] as $event) {
echo '<div>'.$event['name'].'</div>';
Run Code Online (Sandbox Code Playgroud)
它不会反转输出序列.
我究竟做错了什么?还有其他/更好的方法吗?
PS - 我可以用Javascript做,但我需要在服务器端做.
你做了回归,但在错误的领域.您想要反转data字段而不是数组:
$json = file_get_contents($url,0,null,null);
$tmp = json_decode($json, true); // using a temp variable for testing
$result = $tmp;
$result['data'] = array_reverse($result['data']);
foreach ($result['data'] as $event) {
echo '<div>'.$event['name'].'</div>';
Run Code Online (Sandbox Code Playgroud)
您需要反转$tmp['data']数组的内容,而不是$tmp自身.
$json = file_get_contents($url);
$tmp = json_decode($json, true);
$result = array_reverse($tmp['data']);
unset($tmp);
foreach ($result as $event) {
echo '<div>'.$event['name'].'</div>';
}
Run Code Online (Sandbox Code Playgroud)