0

Here is what I have tried:

$arrayToJson = '{"doctype":"invrec","docnum":3006}';
$arrayToJson = json_encode($arrayToJson);

print_r($arrayToJson['docnum']);

The result is: "

DigiNet Events
  • 357
  • 2
  • 14

2 Answers2

4

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.

Martin
  • 16,093
  • 1
  • 29
  • 48
1

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.

04FS
  • 5,660
  • 2
  • 10
  • 21