我使用短信服务提供商发送和接收消息。下面是成功地从短信服务器检索消息并显示在我的网站
上的代码的问题是,当它显示这些数据在一个大块,对不起,我不是一个伟大的程序员,因此我想分割每个文本消息由几个空格,
你能帮我写一个json脚本,让我分裂我的结果,干杯
您需要将JSON从字符串解码为数组或对象,使用json_decode()它有第二个参数bool(当TRUE时,返回的对象将被转换为关联数组)
所以像 (Object):
$responseObj = json_decode($response);
foreach ( $responseObj as $key => $value )
echo "$key = $value<br>";
比如 (Array):
$responseArray = json_decode($response, true);
foreach ( $responseArray as $key => $value )
echo "$key = $value<br>";
首先使用php的json_decode
函数,然后它将出现在array where at "messages" index
中,您可以找到您的所有消息。之后,你必须为每个循环运行,然后你可以让你的代码工作。
$arr = json_decode($response, true);
foreach($arr['messages'] as $message){
echo $message['message'];
}
<?php
$response = json_decode($response);
foreach($response['messages'] as $msgObj) {
print $msgObj->message . "<br/><br/>";
}
?>