Laravel 5.3 - 基于2个匹配键的合并数组?

时间:2017-04-05 22:16:42

标签: php arrays laravel laravel-5.3

鉴于以下两个数组,它们如何有效地合并以产生第三个数组? 的 productData

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

2 个答案:

答案 0 :(得分:3)

我不太了解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

答案 1 :(得分:0)

我建议检查此包dingo/api。我假设你想要显示某种JSON响应。看看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属性以执行相同的操作。