Reputation: 3284
$arr = array();
$arr[0] = "2a123";
$arr[1] = "2123";
$arr["other_option"] = "2123";
var_dump($arr);
$arr = json_encode($arr);
$arr = (array)json_decode($arr);
var_dump($arr);
var_dump( $arr[1]);
var_dump( $arr["1"]);
The output of 2 last var_dump are NULL NULL, if we remove the 4th line $arr["other_option"] = "2123"; it'll ouput correctly, but I don't understand why!
Upvotes: 0
Views: 171
Reputation: 54050
instead of type casting to array , set true
in json_encode
When TRUE, returned objects will be converted into associative arrays.
$arr = array();
$arr[0] = "2a123";
$arr[1] = "2123";
$arr["other_option"] = "2123";
$arr = json_encode($arr);
$arr = json_decode($arr,true);
var_dump( $arr['other_option']); // return 2123
Upvotes: 2