0
$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!

Hieu Vo
  • 3,105
  • 30
  • 31

1 Answers1

2

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

working DEMO

xkeshav
  • 53,360
  • 44
  • 177
  • 245