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

1
  • 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 1, 2019 at 10:06

1 Answer 1

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.