-3

I have this code:

$JSONData = json_decode("[\"tes\", \"dfds\", \"array\"]");

print_r("Data = " . $JSONData);

It outputs:

Data = Array

How do i print the array? Like:

array(4) {
  [0]=>
  string(3) "tes"
  [1]=>
  string(4) "dfds"
  [2]=>
  string(5) "array"
}

2 Answers2

6
print_r("Data = " . $JSONData);

Your problem is that you are transforming the array into a string by concatinating it with another string.

By the time the expression reaches print_r, the array structure has been destroyed.

You should do this in two steps:

print "Data = ";
print_r($JSONData);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

json_decode() returns an array instead of a string. In your case, you can use var_dump() to get the requested output.

  • 2
    Simply substituting `var_dump` for `print_r` will have the same problem as the code in the question. – Quentin Oct 05 '17 at 09:12
  • Well yeah, you can't concatenate the same way as with a simple echo. If the "Data = " part is really needed it needs to be on it's own line in the PHP code. Also, the question was how the array could be printed. `var_dump` gives the exact requested output. For this reason I explicitly advised the use of `var_dump` and not `print_r`. – stuiterveer Oct 05 '17 at 09:15