简体   繁体   English

PHP从另一个数组创建数组

[英]PHP create array from another array

I have an array which I have received from an API. 我有一个从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. 我想遍历数组以在PriceResponse添加另一个对象(即customPrice),并从数组中删除一些对象,例如fPricepriceType等。我想做到这一点的最佳方法是创建另一个数组。 However I can't seem to get it working: 但是我似乎无法使其工作:

PHP: 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: 这是修改后的数组的输出-它仅显示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. $output_array是单个对象,而不是数组。 You're looping over the properties, but never doing anything with $j or $item . 您正在遍历属性,但是永远不要对$j$item做任何事情。

And instead of creating a new array, you can simply modify the objects in the original priceResponse array. 而且,除了创建新的数组之外,您还可以简单地修改原始priceResponse数组中的对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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