When i use JSON::decode,it decodes to a string.How can i convert that to an array?
When i use JSON::decode,it decodes to a string.How can i convert that to an array?
What is the string that you are passing to it?
my json file type:
{"AC":"Ascension Island","AD":"Andorra","AE":"United Arab Emirates","AF":"Afghanistan"....
Well i have tested that code
public function actionTest(){
$cars=array("Saab","Volvo","BMW","Toyota");
echo '<pre>';
echo "Car:<br/>";
print_r($cars);
echo '</pre>';
$encode=CJSON::encode($cars);
echo $encode;
$decode=CJSON::decode($encode);
echo '<pre>';
print_r($decode);
echo '</pre>';
}
And it generate This output
Car:
Array
(
[0] => Saab
[1] => Volvo
[2] => BMW
[3] => Toyota
)
["Saab","Volvo","BMW","Toyota"]
Array
(
[0] => Saab
[1] => Volvo
[2] => BMW
[3] => Toyota
)
And i dont think there is any prob with json encode/decode process with yii.
ok.The idea is to sort a json file.For that i use this code:
$paises = file_get_contents(Yii::getPathOfAlias('webroot.assets') . DIRECTORY_SEPARATOR ."country.json");
$jsondecode=CJSON::decode($paises,true);
uasort($jsondecode, 'sort');
but i get this error on uasort
sort() expects parameter 1 to be array, string given
This isn’t a JSON::decode() issue. You are using uasort() wrong; the array gets converted to a string for the comparison and it’s not getting a comparison function.
You want to use asort() instead, like so:
$paises = file_get_contents(Yii::getPathOfAlias('webroot.assets') . DIRECTORY_SEPARATOR . "country.json");
$jsondecode = CJSON::decode($paises, true);
asort($jsondecode);
Thank you