Php 不理解数组从两个数组创建关联数组
我有两个这样的阵列:Php 不理解数组从两个数组创建关联数组,php,arrays,Php,Arrays,我有两个这样的阵列: $array_one = array('one', 'two', 'three'); $array_two = array(array('one', 'one', 'one'), array('two', 'two', 'two'), array('three', 'three', 'three')) 然后,我需要一个关联数组,将每个名称与数组匹配: array( 'one' => array('one', 'one', 'one'), // ...
$array_one = array('one', 'two', 'three');
$array_two = array(array('one', 'one', 'one'), array('two', 'two', 'two'), array('three', 'three', 'three'))
然后,我需要一个关联数组,将每个名称与数组匹配:
array(
'one' => array('one', 'one', 'one'),
// .... and so on
);
问题?
我认为这很容易做到:
some_storage = array();
foreach($array_one as $key){
foreach($array_two as $value){
$some_storage[$key] = $value;
}
}
但显然我在某些方面失败了,因为最终的结果是:
array(
'one' => array('three', 'three', 'one'),
'two' => array('three', 'three', 'three'),
'three' => array('three', 'three', 'three'),
);
我知道修复是超级简单-但我不知道它是什么 这看起来像是可以做的事情。这看起来像是可以做的事情。我认为您的问题是嵌套循环会遍历数组2的所有值 为什么不试试for循环:
for ($i = 0; $i < count($array_one); $i += 1) {
$some_storage[$array_one[$i]] = $array_two[$i];
}
for($i=0;$i
我认为您的问题在于嵌套循环会遍历数组2的所有值
为什么不试试for循环:
for ($i = 0; $i < count($array_one); $i += 1) {
$some_storage[$array_one[$i]] = $array_two[$i];
}
for($i=0;$i