2017-04-05 55 views
0

给定以下两个数组,他们如何有效地合并以产生第三个数组? productDataLaravel 5.3 - 基于2个匹配键合并数组?

$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_id匹配组合和type/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 
} 
+1

这是不是在这里的PHP代码。你试图自己解决这个问题? – miken32

+0

它是在PHP(Laravel)中,我有这个问题,这是一个laravel集合输出。我尝试了几种不同的方式,但无法完成第三个数组的格式,并且实现时间太长。这就是为什么我在'php/laravel'下发布了这个问题,因为知道laravel的人可能知道如何做到这一点。 – Wonka

+0

您是否试图为API做出JSON响应?如果是,我可能会有解决方案。 –

回答

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

+0

谢谢你,它真的接近最终的输出,但我真的试图从你的沙箱中输出'$ productsFinal'(我的最终输出)中的确切语法来匹配最终输出。目前有两个不同之处,即“数据”:['和它将索引添加到每个产品的事实。任何想法如何改变你的逻辑匹配'$ productsFinal'? – Wonka

+0

好的,我更新了我的答案。 – Gerfried

+0

工作得很好,谢谢:) – Wonka

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" 
    }, 
}, 

然后,您可以还创建一个变压器产品来满足您的规格属性护理做同样的事情。