PHP: 我怎样才能拥有一个具有相同键的数组?
PHP: How can I have an array with same keys?
我有 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 ]
]
您希望 array in PHP 具有相同密钥的两个或多个实例,这根本不可能!
键应被视为唯一实体
我有 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 ]
]
您希望 array in PHP 具有相同密钥的两个或多个实例,这根本不可能!