Here is what I have tried:
$arrayToJson = '{"doctype":"invrec","docnum":3006}';
$arrayToJson = json_encode($arrayToJson);
print_r($arrayToJson['docnum']);
The result is: "
Here is what I have tried:
$arrayToJson = '{"doctype":"invrec","docnum":3006}';
$arrayToJson = json_encode($arrayToJson);
print_r($arrayToJson['docnum']);
The result is: "
You've made a mistake with how you are trying to convert the object to an associative array in your PHP.
You should be using json_decode
to decode the string to an object. Additionally, you want it to be an associative array so must specify TRUE
for the second parameter of json_decode
:
$arrayToJson = '{"doctype":"invrec","docnum":3006}';
$arrayToJson = json_decode($arrayToJson, TRUE);
print_r($arrayToJson['docnum']);
OUTPUT:
3006
For your reference, the documentation for json_decode
is here.
First of all, you want to decode your JSON, not encode it a second time.
And secondly, your JSON contains an object, so you either need to use $arrayToJson->docnum
here to access the property, or you need to decode it with the second parameter of json_decode
set to true
, so that the result will be converted into an array.