Php 如何使用json分割结果

Php 如何使用json分割结果,php,json,Php,Json,im使用短信服务提供商发送和接收消息。以下是成功从文本服务器检索消息并显示在我的网站上的代码 问题是当它把这些数据显示成一大块时,对不起,我不是一个伟大的程序员,所以我想把每条短信分成几个空格 请你帮我写一个json脚本,让我可以分割我的结果,干杯 <?php $response = json_decode($response); foreach($response['messages'] as $msgObj) { print $msgObj->me

im使用短信服务提供商发送和接收消息。以下是成功从文本服务器检索消息并显示在我的网站上的代码

问题是当它把这些数据显示成一大块时,对不起,我不是一个伟大的程序员,所以我想把每条短信分成几个空格

请你帮我写一个json脚本,让我可以分割我的结果,干杯


<?php
    $response = json_decode($response);
    foreach($response['messages'] as $msgObj) {
        print $msgObj->message . "<br/><br/>";
    }
 ?>

您需要将字符串中的JSON解码为数组或对象,使用它的第二个参数bool(如果为TRUE,则返回的对象将转换为关联数组)

比如(对象):

$responseObj=json\u decode($response);
foreach($responseObj作为$key=>$value)
回显“$key=$value
”;
比如(数组):

$responseArray=json\u decode($response,true);
foreach($responseArray为$key=>$value)
回显“$key=$value
”;
首先使用php的
json\u decode
功能,然后它将出现在
数组中,在“messages”索引中可以找到所有消息。之后,您必须为每个循环运行,然后才能使代码正常工作

$arr = json_decode($response, true);

foreach($arr['messages'] as $message){
 echo $message['message'];
}

回答不好,所以只需复制和粘贴,这应该适用于OP,不需要json_解码否?
$response
是一个字符串,应该
$response['messages']
给出什么?旁白:键是
消息
,而不是
消息
。@axelam,索引是一个位置的消息和另一个位置的消息。使用json_解码后的解决方案有什么问题?@Loz Cherone:谢谢“增强”。但这是一种“请做我的家庭作业”的问题,因此我仅限于min。谢谢各位,但这个脚本没有显示任何消息:s在messages之前突出了第一部分。可能需要根据当前实际的JSON字符串调整数组/对象引用。这就是为什么我强调“喜欢…”嘿,拉维,我用你的代码做了我想要的:)我添加了br init:$arr=json_decode($response,true);foreach($arr['messages']as$message){echo$message['message'].“

”;}这很好,但我也希望日期出现在脑海中,如果您可以添加日期部分,我尝试了,但在第行出现错误,感谢您必须做以下事情:$arr=json_decode($response,true);foreach($arr['messages']作为$message){echo$message['message'].“

;“echo$message['date'.”
;}
$responseArray = json_decode($response, true);
foreach ( $responseArray as $key => $value )
    echo "$key = $value<br>";
$arr = json_decode($response, true);

foreach($arr['messages'] as $message){
 echo $message['message'];
}