Ad

Displaying Json Array In Echo Which Is Nested Array Data In Blade Laravel

- 1 answer

this is my code, I want to echo all the data inside this $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
}
?>

the problem come went the data loop on session_from_at which have three data in array inside.

Ad

Answer

You got an error because other $value is an array you cant echo it so try to check if its array or object like this :

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>";
    }

}
Ad
source: stackoverflow.com
Ad