shanmugakumar s shanmugakumar s - 2 months ago 14
PHP Question

numeric Json decode not work

i have below json code

{"1":1,"5":1}


when i decode the above json i got object array using the below php statement.

$array_val = (array)json_decode($price);


i got a below array.

Array
(
[1] => 1
[5] => 1
)


but the below statement does not work

echo $array_val[1];


the below error occurred.

Undefined offset: 1

How to resolve this issue?

Answer

try this DEMO

PHP

$json = '{"1":1,"5":1}';
$array_val=json_decode($price, true);
echo $array_val[1];

OUTPUT :

1
Comments