如何在PHP

时间:2017-03-13 07:43:38

标签: php json

{"senderDetails":[{"status":"failed","senderid":"0","message":"Kindly upgrade your account to use this service."}]}

以上是我的JSON数据。我使用json_decode()函数进行解码,但我不知道如何在PHP中单独回显每个对象的值。

4 个答案:

答案 0 :(得分:1)

例如,如果要打印senderid:

<?php
$var = '{"senderDetails":[{"status":"failed","senderid":"0","message":"Kindly upgrade your account to use this service."}]}';

$decoded = json_decode($var);

var_dump($decoded);
echo($decoded->senderDetails[0]->senderid);

?>

在这些变量上使用var_dump()以了解如何正确访问它们非常重要。

答案 1 :(得分:0)

如果要回显所有值,可以循环遍历数组,如下所示:

foreach($your_array as $value){
    echo $value . "<br>";
}

但是,如果您想输出特定元素,您可以这样做:

echo( $your_array->object->value );

答案 2 :(得分:0)

如果您使用

$json_array = json_decode($json,true) //If you add true you get an array as Response
$value = $json_array["senderDetails"]["status"]; // Example

答案 3 :(得分:0)

您可以转换为数组并以这种方式回显变量

$json_data = '{"senderDetails":[{"status":"failed","senderid":"0","message":"Kindly upgrade your account to use this service."}]}';
$decoded = json_decode($json_data,true);

echo "Status = ".($decoded["senderDetails"][0]["status"]);
echo "<br />";
echo "Senderid = ".($decoded["senderDetails"][0]["senderid"]);
echo "<br />";
echo "Message = ".($decoded["senderDetails"][0]["message"]);