2

The below method have changed property instance in object into array but the requirement have different, the result of response will be same required after manipulation.

<?php

$data =  array('statusCode'=>200,
               'statusDescripion'=>'success',
               'data'=> array('companyImage'=>new StdClass(),
               'profile'=>array())
              );

echo "<br><br> Encoded data coming from API<br>";
echo $encodeData = json_encode($data,true);

//echo "<br><br> Decode data for Manipulation <br>";
$decodeData = json_decode($encodeData,true);
//print_r($decodeData);
if($decodeData['statusCode'] == 200){
    $data_ = array('statusCode'=>$decodeData['statusCode'],
                   'statusDescripion'=>$decodeData['statusDescripion'],
                   'data'=>$decodeData['data'],
                   'url'=>"php.com");
}

echo "<br><br> After Manipulation <br>";
echo json_encode($data_,true);

enter image description here

Query Master
  • 6,989
  • 5
  • 35
  • 58
  • 2
    You change it to an array in PHP by passing true in `json_decode`, so you'll either need to change it back manually, or don't pass true, and update the values by accessing properties instead of array key. By the by, what's with the `true` in `json_encode`? – Jonnix Sep 01 '19 at 10:06

1 Answers1

4

From json-decode documentation:

When TRUE, returned objects will be converted into associative arrays

You using it so the object are convert into array - if you want it to be object (aka {}) just remove the true from the line:

$decodeData = json_decode($encodeData,true);

To:

$decodeData = json_decode($encodeData);

And by the way, json-encode doesn't get true as second argument, I think you wanted JSON_FORCE_OBJECT

dWinder
  • 11,597
  • 3
  • 24
  • 39