json_decode() expects parameter 1 to be string, array given

Hello.

Explain to me why I get this error?


$posts = News::model()->findAll(array('select' => 'date'));

$jsons = CJSON::decode($posts);

foreach ($jsons as $json) {

    echo var_dump($json);

}

And I get

When I do so


$posts = News::model()->findAll(array('select' => 'date'));

foreach ($posts as $post) {

    echo var_dump(CJSON::decode($post));

}

I do not get this error. But I need decoded array of json assigned to a variable and transfer function. How to do it correctly?

Hope this helpful.




$posts = News::model()->findAll(array('select' => 'date'));

$arr=array();

foreach ($posts as $post) {

    $arr[]=CJSON::decode($post);

}



Ok, unless I am not missing something obvious you are confusing DEcoding with ENcoding. If you want your models to be serialized as a JSON string you’ll have to write


$jsonString=CJSON::encode($models);

Getting a php array out of a JSON string would mean


$phpArray=CJSON::decode($jsonString);