填充动态数组

时间:2011-08-17 20:57:51

标签: php arrays

如何在for循环中动态填充以下类型数组

$trans2 = array(); 
$username = array("John","Steve"); 
$categories = arra("cat1","cat2","cat3");

结果必须是:

[0] => Array ( [Name] => John [cat1] => 0 [cat2] => 0 [cat3] => 0 )

[1] => Array
(
    [Name] => Smith
    [cat1] => 0
    [cat2] => 0
    [cat3] => 0
)

提前致谢!

2 个答案:

答案 0 :(得分:0)

foreach($username as $name)
{
   foreach($categories as $cat)
   {
      $trans2[$name][$cat] = 0;
   }
}

答案 1 :(得分:0)

尝试类似的东西

foreach($username as $key=>$name){
  $trans2[$key]['name'] = $name;
  foreach($categories as $cat){
    $trans2[$key][$cat] = 0;
  }
}