如何在foreach循环中创建多维关联数组?

时间:2012-05-03 14:03:57

标签: php

我想创建一个看起来像这样的数组

 $appsByOs = Array(
                  osName1 =>Array(0=>app1,1=>app2)
                  osName2 =>Array(0=>app1,1=>app2)
                  etc.....
  )

我怎么能在foreach循环中做到这一点?

  foreach($osInstanceNames as $osInstanceName){
                    $appNames(array of app names for current os)

            }

感谢

2 个答案:

答案 0 :(得分:0)

你需要做的就是这个。

 $appsByOs = Array();
 foreach($osInstanceNames as $osInstanceName){
                     $appsByOs[$osInstanceName] = $appNames;

 }

答案 1 :(得分:0)

恢复上一篇文章并添加为os实例提供多个应用程序的可能性

 $appsByOs = Array(); 
 foreach($osInstanceNames as $osInstanceName){ 
                     $appsByOs[$osInstanceName][] = $appNames; 

 }