Laravel 5.3 - 根据 2 个匹配键合并数组?

Laravel 5.3 - Merge array based on 2 matching keys?

给定以下两个数组,如何有效合并它们以产生第三个数组? 产品数据

$productData =
[
  {
    "product_id": 4,
    "type": "electronic",
    "name": "monitor",
    "specs": {
      "HDMI": true,
      "VGA": false
    }
  },
  {
    "product_id": 5,
    "type": "electronic",
    "name": "HDMI cable",
    "specs": {
      "length": "3ft"
    }
  },
  {
    "product_id": 6,
    "type": "kitchen",
    "name": "spoon"
  }
]

产品

$products =
{
  "products": 3,
  "per_page": 10,
  "current_page": 1,
  "data": [
    {
      "id": 4,
      "product_type": "electronic",
      "product_id": 6
    },
    {
      "id": 6,
      "type": "electronic",
      "product_id": 5
    },
    {
      "id": 9,
      "type": "kitchen",
      "product_id": 4
    }
  ]
}

productsFinal$productData 合并到 $products - 基于 product_id/product_idtype/product_type 的匹配组合)

$productsFinal = 
{
  "products": 3,
  "per_page": 10,
  "current_page": 1,
  "data": [
    {
      "id": 4,
      "product_type": "electronic",
      "product_id": 6,

      // How to merge product data and wrap with "data" key
      "data": {
        "product_id": 6,
        "type": "kitchen",
        "name": "spoon"
      }

    },
    {
      "id": 6,
      "type": "electronic",
      "product_id": 5,

      // How to merge product data and wrap in "data" key
      "data": {
        "product_id": 5,
        "type": "electronic",
        "name": "HDMI cable",
        "specs": {
          "length": "3ft"
        }
      }
    },
    {
      "id": 9,
      "type": "kitchen",
      "product_id": 4,

      // How to merge product data and wrap in "data" key
      "data": {
        "product_id": 6,
        "type": "kitchen",
        "name": "spoon"
      }
    }
  ]
}

我在 foreach 循环中尝试了不同的结果,但仍然无法按预期呈现:

foreach($productData as $productDataItem) {
  // when $productDataItem.product_id == $product.product_id && $productDataItem.type == $product.product_type
  // move the matching $productDataItem object into matching $product object, wrapped in a new "data" key
}

我建议检查这个包 dingo/api. I assume you want to display some kind of JSON response. Take a look at Transformers。你可以这样做:

<?php
namespace App\Http\Transformers;
use App\Http\Controllers\ProductData;
use League\Fractal\TransformerAbstract;

class ProductsDataTransformer extends TransformerAbstract
{
    /**
     * Turn this item object into a generic array
     *
     * @return array
     */
    public function transform(ProductData $productdata)
    {
        return [
            'id' => $productdata->id,
            'product_type' => $productdata->product_type,
            'product /*or data*/' => Product::find($productdata->product_id),
        ];
    }

}

这会通过 ID 找到产品,如下所示:

{
  "id": 4,
  "product_type": "electronic",
  "product" {
    "product_id": 6,
    "type": "kitchen",
    "name": "spoon"
  },
},

然后您还可以为 Product 创建一个转换器来处理您的 specs 属性来做同样的事情。

我不太了解Laravel。然而,您可以很容易地加入您的数据对象:

<?php
$productData = json_decode('[
  {
    "product_id": 4,
    "type": "electronic",
    "name": "monitor",
    "specs": {
      "HDMI": true,
      "VGA": false
    }
  },
  {
    "product_id": 5,
    "type": "electronic",
    "name": "HDMI cable",
    "specs": {
      "length": "3ft"
    }
  },
  {
    "product_id": 6,
    "type": "kitchen",
    "name": "spoon"
  }
]');

$products = json_decode('{
  "products": 3,
  "per_page": 10,
  "current_page": 1,
  "data": [
    {
      "id": 4,
      "type": "electronic",
      "product_id": 6
    },
    {
      "id": 6,
      "type": "electronic",
      "product_id": 5
    },
    {
      "id": 9,
      "type": "kitchen",
      "product_id": 4
    }
  ]
}');

// combine both data objects 

foreach($products->data As &$p) {
    foreach($productData As $d) {
        if(property_exists($p, "product_id") && property_exists($d, "product_id") && property_exists($p, "type") && property_exists($d, "type")) {
            if($p->product_id==$d->product_id && $p->type==$d->type) {
                //$p = (object) array_merge((array) $p, (array) $d);
                $p->data = $d; // updated answer
                continue;
            }
        }
    }
}

echo("<pre>");
echo json_encode($products, JSON_PRETTY_PRINT);


?>

您可以在此处测试代码:http://sandbox.onlinephpfunctions.com/code/98a50c35ee32c30f0d2be1661f7afb5895174cbe 更新:http://sandbox.onlinephpfunctions.com/code/aeebfdcf4f4db5e960260e931982570cfed19e0e