Search code examples
phparraysobjectmultidimensional-arraysub-array

Calculate a new column and remove other columns within nested objects of a multidimensional object


I receive an object from an API which has this structure:

{
    "success": true,
    "quoteId": 11,
    "abcValue": 0,
    "priceResponse": [
        {
            "priceId": 1263,
            "fPrice": 37.14,
            "grossPrice": 44.7,
            "priceType": "ABC"
        },
        {
            "priceId": 1263,
            "fPrice": 37.14,
            "grossPrice": 44.7,
            "priceType": "ABC"
        },
        {
            "priceId": 1266,
            "fPrice": 550.14,
            "grossPrice": 544.7,
            "priceType": "DEF"
        }
    ]
}

I want to loop through the PriceResponse array to add a new calculated property and remove unneeded properties from each child object. The best way I figured to do this was to create another array. However I can't seem to get it working.

My coding attempt:

$output_array = json_decode($output);
$modified_array = array();
$priceResultArray = array();

foreach($output_array as $j => $item) {
                
    foreach($output_array->priceResponse as $i => $field) {
        $percent =  $field->grossPrice * 10 / 100;
        $customPrice =  $field->grossPrice + $percent;

        $priceResultArray['priceId'] = $field->priceId;
        $priceResultArray['customPrice'] = $customPrice;
    }

    $modified_array['success'] = $output_array->success;
    $modified_array['quoteId'] = $output_array->quoteId;
    $modified_array['priceResponse'] = $priceResultArray;
}
var_dump($modified_array);

This is the output of the modified array - it only shows the last result of the priceResultArray:

array(3) {
  ["success"]=>
  bool(true)
  ["quoteId"]=>
  int(0011)
  ["priceResult"]=>
  array(5) {
    ["priceId"]=>
    int(1266)
    ["customPrice"]=>
    float(599.17)
  }
}

Solution

  • You don't need the outer loop. $output_array is a single object, not an array. You're looping over the properties, but never doing anything with $j or $item.

    And instead of creating a new array, you can simply modify the objects in the original priceResponse array.

    $output_array = json_decode($output);
    foreach ($output_array->priceResponse as $field) {
        $percent =  $field->grossPrice * 10 / 100;
        $customPrice =  $field->grossPrice + $percent;
        $field->customPrice = $customPrice;
        unset($field->fPrice);
        unset($field->priceType);
        unset($field->grossPrice);
    }