简体   繁体   English

组合基于偏移值的数组

[英]Combining an array based off value

Alright, I have been pulling my hair out on this one! 好吧,我一直在拉我的头发! I have tried an SQL call to add the features to a product, but since the product is one row and the features are not it creates a new row for each product. 我曾尝试通过SQL调用将功能添加到产品中,但是由于该产品是一行,而功能不是,因此会为每个产品创建一个新行。 I thought I could add both to an array and join them based off the product ID. 我以为我可以将两者都添加到数组中,然后根据产品ID加入它们。 Here are the arrays 这是数组

array
  0 => 
    array
  'id_product' => string '9' (length=1)
  'name' => string 'Brady ENV100 MAXX Enhanced Sorbents' (length=35)
  'number_sold' => string '6' (length=1)
  'link_rewrite' => string 'brady-env100-maxx-enhanced-sorbents' (length=35)
  'id_image' => string '27' (length=2)
 1 => 
array
  'id_product' => string '10' (length=2)
  'name' => string 'Brady GP100 MAXX Enhanced Heavy Pad' (length=35)
  'number_sold' => string '3' (length=1)
  'link_rewrite' => string 'brady-gp100-maxx-enhanced-heavy-pad' (length=35)
  'id_image' => string '29' (length=2)

array
 0 => 
array
  'id_product' => string '9' (length=1)
  'name' => string 'Height' (length=6)
  'value' => string '5' (length=1)
 1 => 
array
  'id_product' => string '9' (length=1)
  'name' => string 'Width' (length=5)
  'value' => string '5' (length=1)
 2 => 
array
  'id_product' => string '9' (length=1)
  'name' => string 'Depth' (length=5)
  'value' => string '5' (length=1)
  3 => 
array
  'id_product' => string '9' (length=1)
  'name' => string 'Weight' (length=6)
  'value' => string '5' (length=1)
 4 => 
array
  'id_product' => string '10' (length=2)
  'name' => string 'Height' (length=6)
  'value' => string '10' (length=2)

I have looked at many tutorials, but can't seem to get anything to work. 我看过许多教程,但似乎什么都无法工作。 What I would like it to look like is 我希望它看起来像是

array
0 => 
array
'id_product' => string '9' (length=1)
'name' => string 'Brady ENV100 MAXX Enhanced Sorbents' (length=35)
'number_sold' => string '6' (length=1)
'link_rewrite' => string 'brady-env100-maxx-enhanced-sorbents' (length=35)
'id_image' => string '27' (length=2)
name' => string 'Height' (length=6)
'value' => string '5' (length=1)
 'name' => string 'Width' (length=5)
'value' => string '5' (length=1)
'name' => string 'Depth' (length=5)
'value' => string '5' (length=1)

and so on. 等等。

You can try this 你可以试试这个

For your product array do this: 对于您的产品阵列,请执行以下操作:

$result = array();
foreach ($mainItems as $item){
    $result[$item["id_product"]] = $item;
}

Then for each of your attribute arrays 然后对于每个属性数组

foreach($attributes as $val){
    if (array_key_exists($val["id_product"], $result)){
        $result[$val["id_product"]][$val["name"]] = $val["value"];
    }
}

You should end up with an array that looks like 您应该以一个看起来像下面的数组结束

[9] => array(
       [id_product] => 9
       [name] => "Brady ENV100 MAXX Enhanced Sorbents"
       [number_sold] => "6"
       [link_rewrite] => "brady-env100-maxx-enhanced-sorbents"
       [id_image] => "27"
       [Height] => 5
       [Width] => 5
       [Depth] => 5  
),
[10] => array(
       [id_product] =>  '10' 
       [name] =>  'Brady GP100 MAXX Enhanced Heavy Pad' 
       [number_sold] => '3'
       [link_rewrite] =>  'brady-gp100-maxx-enhanced-heavy-pad' 
       [id_image] =>  '29'

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

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