【问题标题】:PHP - adding to multi dimensional array adds new arrays to the first array element rather than the correct elementPHP - 添加到多维数组将新数组添加到第一个数组元素而不是正确的元素
【发布时间】:2020-12-14 19:35:13
【问题描述】:

我正在读取一个“父”数组,并根据我在该父数组的子数组中读取的一些值创建一个新的子数组。即我将子数组中“产品”的数量相加以创建一个新的子数组。 新子数组正在正确创建,但不是在相应的父数组元素中创建新子数组,我的逻辑是将所有新子数组放在父数组的第一个元素中。请帮忙。

foreach ($orders as $order) {
    
   $orderResource = new OrderResource(Order::findOrFail($order['id']));         
   $ordersAll[] = $orderResource;
   
   foreach (($orderResource['products']) as $product) {
    if (array_key_exists($product['productType'],$product_array)) {
        // array_push($product_array, $product['productType']);
        $product_array[$product['productType']] += (int)$product['quantity'];
    }
    else $product_array[$product['productType']] = (int)$product['quantity']; 
    }
    
    $ordersAll['product'][]= $product_array;
    $product_array=[];

} 

结果:不是在每个数组元素中都有一个新的“产品”数组,而是它们都在索引 0 中。

    array:22 [▼
  0 => array:9 [▶]
  "product" => array:21 [▶]
  1 => array:9 [▶]
  2 => array:9 [▶]
  3 => array:9 [▶]
  4 => array:9 [▶]
  5 => array:9 [▶]
  6 => array:9 [▶]
  7 => array:9 [▶]
  8 => array:9 [▶]
  9 => array:9 [▶]
  10 => array:9 [▶]
  11 => array:9 [▶]
  12 => array:9 [▶]
  13 => array:9 [▶]
  14 => array:9 [▶]
  15 => array:9 [▶]
  16 => array:9 [▶]
  17 => array:9 [▶]
  18 => array:9 [▶]
  19 => array:9 [▶]
  20 => array:9 [▶]
]

所以,我希望每个数组索引都包含一个新的“product_sum”数组。这将包含找到的所有产品类型的键值和“数量”的总和。

当前:

0 => array:9 [▼
    "refId" => "8e24cc331a73002b0c2da4bc7a62c647"
    "Currency" => "USD"
    "Symbol" => "$"
    "Amount" => "315.50"
    "created_at" => "2020-12-08T03:32:09.000000Z"
    "updated_at" => "2020-12-08T03:32:09.000000Z"
    "legs" => array:2 [▶]
    "travellers" => array:3 [▶]
    "products" => array:3 [▼
      0 => array:13 [▶]
      1 => array:13 [▶]
      2 => array:13 [▶]
    ]
  ]

我所追求的是(在每个父数组索引中):

0 => array:9 [▼
"refId" => "8e24cc331a73002b0c2da4bc7a62c647"
"Currency" => "USD"
"Symbol" => "$"
"Amount" => "315.50"
"created_at" => "2020-12-08T03:32:09.000000Z"
"updated_at" => "2020-12-08T03:32:09.000000Z"
"legs" => array:2 [▶]
"travellers" => array:3 [▶]
"products" => array:3 [▼
  0 => array:13 [▶]
  1 => array:13 [▶]
  2 => array:13 [▶]
"product_sum" => array:1 [▼
  0 => array:2 [▼
   "ticket" => 5
   "bag" => 1

【问题讨论】:

  • 你在那个输出中倾倒了什么?还有为什么$ordersAll['product'][]= $product_array;
  • 如果您想要在每个数组元素($ordersAll)中添加一个新的产品数组,那么您可能需要执行$product['product'][] = $product_array,因为 $product 是 foreach 循环中数组的单个元素

标签: php laravel foreach


【解决方案1】:

此行将所有产品数组强制放入名为 product$ordersAll 的元素中:

$ordersAll['product'][]= $product_array;

不确定为什么要以这种方式与资源交互,但您可以重新排序以消除知道$ordersAll 的当前索引的问题:

foreach ($orders as $order) {
    
    $orderResource = new OrderResource(Order::findOrFail($order['id']));         

    $product_array = [];

    foreach ($orderResource['products'] as $product) {
        if (array_key_exists($product['productType'], $product_array)) {
            $product_array[$product['productType']] += (int)$product['quantity'];
        } else {
            $product_array[$product['productType']] = (int)$product['quantity']; 
        }
    }
    
    // TODO:
    // need the code to add $product_array to $orderResource
    $ordersAll[] = $orderResource;
} 

虽然您应该能够从几个 Collection 方法构建此列表,而无需自己循环任何内容:

Order::findOrFail(...)->products->groupBy('productType')->map->sum('quantity');

【讨论】:

  • 感谢您的建议。我会看看用 Collection 方法解决它时能找到什么。与此同时,我使用了您的解决方案,虽然它给了我数组 - 产品类型:'数量' 它不会在父数组中返回它们。我的目标是在每个父订单中都有这个新数组。
  • 作为父级的什么字段?因为OrderResource 不是数组
  • 每个订单有几个属性加上 3 个数组(腿、旅行者、产品)。理想情况下,我想在现有的“产品”数组下添加新数组。
  • 怎么样? products 是模型的集合,我会假设哪些模型正在通过自己的资源进行转换?否则根本不知道为什么要使用资源
  • 我认为需要 Resource 来获取与 Order 相关的模型(腿、产品、旅行者)。
【解决方案2】:

我相信这是问题

$ordersAll[] = $orderResource;
    ...
$ordersAll['product'][]= $product_array;

您将订单添加到 ordersAll 数组,但每次都添加和覆盖相同的产品密钥。

【讨论】:

    猜你喜欢
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多