在回显中显示Json数组,这是刀片laravel中的嵌套数组数据

时间:2019-12-18 02:21:11

标签: php json laravel foreach laravel-blade

这是我的代码,我想回显$ json中的所有数据。

$json = '{"year":"2018","month":"4","name":"Apr 2018 - Sept 2018","description":null,"code":"2018\/04","session_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"session_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_to_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"added_dropped_from_at":null,"added_dropped_to_at":null,"withdrew_from_at":null,"withdrew_to_at":null,"attended_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"attended_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_from_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"created_by":1,"updated_by":1,"id":2}';

$arr = json_decode($json,true);

foreach($arr as $key=>$value){
    echo $key . "<br>";
    echo $value . "<br>"; // PHP Notice:  Array to string conversion in /workspace/Main.php on line 11
// not displaying the value
}
?>

问题来了,session_from_at上的数据循环进入了数组,里面有三个数据。

3 个答案:

答案 0 :(得分:2)

您遇到了错误,因为其他$ value是无法回显的数组,因此请尝试检查其数组或对象是否像这样:

foreach($arr as $key=>$value){
    echo $key . "<br>";
    if(is_object($value) || is_array($value)){
        foreach($value as $key2=>$value2){
             echo $key2 . "<br>";
             echo $value2 . "<br>";
        }
    }else{
        echo $value . "<br>";
    }

}

答案 1 :(得分:1)

我认为您没有使用刀片。您的文件名为Main.php 如果您使用刀片服务器,建议您使用@foreach而不是foreach

json_decode之后,您在数组中仍然有一个数组。

尝试以下代码:

$json = '{"year":"2018","month":"4","name":"Apr 2018 - Sept 2018","description":null,"code":"2018\/04","session_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"session_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_to_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"added_dropped_from_at":null,"added_dropped_to_at":null,"withdrew_from_at":null,"withdrew_to_at":null,"attended_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"attended_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_from_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"created_by":1,"updated_by":1,"id":2}';

$arr = json_decode($json, true);

function echoArray($arr) {
    foreach($arr as $key=>$value){
        echo $key . "<br>";
        if (is_array($value)) {
            echoArray($value);
        } else {
            echo $value . "<br>";
        }
    }
}

echoArr($arr);

答案 2 :(得分:0)

尝试:

 $arr['year'];
 $arr['month'];

您不需要使用foreach。