Noman Ali
Noman Ali

Reputation: 3340

var_dump and print_r displays different results - PHP

I have a string (JSON type), i wanted to convert it to PHP Array.

{  
   "action":"putEntity",
   "dataPacket":{  
      "entity":[  
         {  
            "name":"product",
            "data":[  
               {  }
            ]
         }
      ]
   }
}

I did following to do so,

$array = json_decode(json_encode($data), True);

When i do var_dump($array); it displays:

string(1578) "{ "action": "putEntity", "dataPacket": { "entity": [{ "name": "product", "data": [{ }] }] } }"

But when i do, print_r($array); it displays:

{
   "action": "putEntity",
   "dataPacket":{
   "entity":[
         {
         "name": "product",
         "data":[{}]
         }
      ]
   }
}

Issue is when i try to print $array['dataPacket']; it throws error illegal string offset 'dataPacket'

why var_dump is showing it as String? please help.

Upvotes: 0

Views: 59

Answers (1)

RobIII
RobIII

Reputation: 8821

$array = json_decode(json_encode($data), True);

Should be

$array = json_decode($data, true);

Upvotes: 1

Related Questions