【问题标题】:How to convert array json into string? [duplicate]如何将数组json转换为字符串? [复制]
【发布时间】:2017-01-28 13:39:27
【问题描述】:

如何将数组更改为字符串 uning php?

这是数组

{
    "result": "success", 
    "message": [ 
        { 
            "date_insert": "2017-01-28 20:14:51", 
            "date_update": "2017-01-28 20:15:11", 
            "weather": "sunny"
        }
    ]
}

我想要字符串中的天气输出。谢谢

【问题讨论】:

  • 那不是数组。那是一个json对象。您需要向我们展示您是如何获得该信息的。如果你得到它作为 json,它可能已经是一个字符串。无论哪种方式,请查看 json_encode()json_decode()

标签: php mysql arrays json api


【解决方案1】:

首先使用 json_decode() 和第二个参数 true 将您的 json 转换为数组。然后访问每个数组元素。

<?php

$json = '{
    "result": "success", 
    "message": [ 
        { 
            "date_insert": "2017-01-28 20:14:51", 
            "date_update": "2017-01-28 20:15:11", 
            "weather": "sunny"
        }
    ]
}';

$array = json_decode($json,true);//Now you have an array
//print_r($array);
echo $array['message'][0]['weather'];//outputs sunny

?>

有关更多信息,请参阅文档http://php.net/manual/en/function.json-decode.php

【讨论】:

  • OP的问题是:"convert array json into string"。这会将 json 字符串转换为数组。不过,这个问题还很不清楚。
  • I want the weather output in string 是什么意思?
  • 老实说,我不认为 OP 真的知道。
  • 希望他能编辑更多问题?
猜你喜欢
  • 2019-03-08
  • 2015-08-17
  • 2012-05-03
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-20
相关资源
最近更新 更多