PHP:我怎么能有一个具有相同键的数组?

时间:2015-07-08 16:13:37

标签: php arrays multidimensional-array key associative-array

我有2个数组$projectsIds = [1,2]$usersIds = [1,2,3]

我有这样的foreach循环:

foreach($projectsIds as $projecID){
           foreach($userIds as $userID){
           $sync_array[$projecID] = array('user_id' => $userID);
            }
       }
dd($sync_array);

现在我得到这样的结果:

array:2 [  1 => array:1 [    "user_id" => 1  ]  
           2 => array:1 [    "user_id" => 1  ]
        ]

可以得到这样的结果:

array:2 [  1 => array:1 [    "user_id" => 1  ]  
           1 => array:1 [    "user_id" => 2  ] 
           1 => array:1 [    "user_id" => 3  ]
           2 => array:1 [    "user_id" => 1  ]
           2 => array:1 [    "user_id" => 2  ]
           2 => array:1 [    "user_id" => 3  ]
        ]

1 个答案:

答案 0 :(得分:4)

您希望array in PHP有两个或更多相同密钥的实例,这根本不可能!

键应被视为唯一实体