【问题标题】:PHP json decode array reversePHP json解码数组反向
【发布时间】:2019-11-06 13:50:07
【问题描述】:

我做错了什么?

我正在尝试使用 array_reverse 反转 json_decode,但出现以下错误

警告:array_reverse() 期望参数 1 是数组,给定对象

使用下面的代码:

$data = file_get_contents($url);

$output = json_decode($data);

$output = array_reverse($output);

谢谢。

【问题讨论】:

  • 您的 JSON 数据似乎由一个对象组成,而不是一个数组。简单的解决方法:设置json_decode的第二个参数为true。
  • 你试过$output = (array)json_decode($data)吗?
  • json_decode($data, true);
  • $output = json_decode($data, true); prnt.sc/pt9bd0
  • 也许一个更好的问题可能是你为什么需要反转数组

标签: php json array-reverse


【解决方案1】:

您必须将第二个参数true 放入

$output = json_decode($data, true);

默认 json_decode 将 json 转换为 php-object,如果你将 true 作为参数 - 它应该将它转换为数组

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多