简体   繁体   中英

PHP create array from another array

I have an array which I have received from an API.

object(stdClass)#19498 (4) {
  ["success"]=>
  bool(true)
  ["quoteId"]=>
  int(0011)
  ["abcValue"]=>
  float(00)
  ["priceResponse"]=>
  array(4) {
    [0]=>
    object(stdClass)#19502 (9) {
      ["priceId"]=>
      int(1263)
      ["fPrice"]=>
      float(37.14)
      ["grossPrice"]=>
      float(44.7)
      ["priceType"]=>
      string(2) "ABC"
    }
    [1]=>
    object(stdClass)#19501 (10) {
      ["priceId"]=>
      int(1263)
      ["fPrice"]=>
      float(37.14)
      ["grossPrice"]=>
      float(44.7)
      ["priceType"]=>
      string(2) "ABC"
    }
    [2]=>
    object(stdClass)#19500 (8) {
      ["priceId"]=>
      int(1266)
      ["fPrice"]=>
      float(550.14)
      ["grossPrice"]=>
      float(544.7)
      ["priceType"]=>
      string(2) "DEF"
    }
  }
}

I want to loop through the array to add another object in PriceResponse ie customPrice and also delete some objects from the array like fPrice , priceType etc. The best way I figured to do this was to create another array. However I can't seem to get it working:

PHP:

$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)
  }
}

Any pointers would be appreciated.

You have such output because you put values inside the same keys within your loop. You should create new object on each loop interation. Checkout this code:

 $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; $singlePriceResult = array(); $singlePriceResult['priceId'] = $field->priceId; $singlePriceResult['customPrice'] = $customPrice; $priceResultArray[] = $singlePriceResult; } $modified_array['success'] = $output_array->success; $modified_array['quoteId'] = $output_array->quoteId; $modified_array['priceResponse'] = $priceResultArray; } var_dump($modified_array); 

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM