将另一组结果添加到数组中

时间:2011-11-25 16:07:06

标签: php arrays multidimensional-array

不是100%确定我在这里问的是什么。我想为我已经循环的数组添加另一个级别。

$product_query = mysql_query("SELECT * FROM table");
while ($row = mysql_fetch_assoc($product_query)) {$products[] = $row;}
 foreach($products as $product){
  //external array request is here which then generates this:
  foreach ($Items->Item as $Items){
   echo $Items->title; // I want to add these value**s** to the existing $products array
  }
 }

所以我现在有这个:

Array
(
    [0] => Array
        (
            [product_id] => 1
            [sku] => TPF1
        )

    [1] => Array
        (
            [product_id] => 2
            [sku] => TFL3
        )

我有道理吗?谢谢你的帮助

斯图

1 个答案:

答案 0 :(得分:0)

$product_query = mysql_query("SELECT * FROM table"); 
while ($row = mysql_fetch_assoc($product_query)) {$products[] = $row;} 
 foreach($products as &$product) { //notice the ambersand
     foreach ($Items->Item as $Item){ 
        $product['title'] =  $Item->title; 
  } 
}

现在$ products将包含每个项目标题