-2

I'm sending my posted data to the server as a block of JSON data due to its size. Once it arrives, I decode it using json_decode and then go about my business. The problem I've run into is I can't access values at indexes. When I print_r the array it shows everything, but trying to echo the value at an index shows nothing. Why doesn't the echo work? Here's what I'm trying:

$content = json_decode($_POST['result']);

print_r($content); //works

echo $content["rowCount"]; //doesn't work

Here's what print_r($content); prints out:

stdClass Object ( [prodName] => [company] => [runTime] => [adLine] => [vintage] => [agency] => [vimeo] => [notes] => [displayOrder] => [infoId] => [prodName1] => test [company1] => test [runTime1] => 0 [adLine1] => [vintage1] => 0 [agency1] => [vimeo1] => https://vimeo.com/ [notes1] => [displayOrder1] => 0 [infoId1] => 339 [rowCount] => 1 )

Raviga
  • 117
  • 1
  • 16
  • 1
    _trying to echo the value at an index shows nothing..._ Because there is no array but objects – B001ᛦ Dec 18 '18 at 15:55

3 Answers3

2

By default, json_decode will convert it into a stdClass object. If you need an associative array, pass true as the 2nd argument.

http://php.net/manual/en/function.json-decode.php

<?php

$content = json_decode($_POST['result'], true);
...
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
1

You should try passing true as the second argument to the json_decode() function:

$content = json_decode($_POST['result'], true);

Or ... access the data as an object:

echo $content->rowCount;
Peter
  • 1,615
  • 1
  • 9
  • 17
1

The print_r out is self-explanatory. You see the print_r output says it is a stdClass Object. So, you need to access them using $content->key_name. I am not sure if you have the array in proper format which was used to json_encode.

Abdur Rahman
  • 762
  • 6
  • 17