我有两个看起来像这样的数组:
$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'),
);
我知道修复程序非常简单 - 但我不知道它是什么......
这
看起来像是可以用array_combine完成的事情。
$array_result = array();
foreach ($array_one as $key => $val)
$array_result[$val] = $array_two[$key];
我认为您的问题是您的嵌套循环贯穿数组 2 的所有值为什么不尝试一个 for 循环:
for ($i = 0; $i < count($array_one); $i += 1) {
$some_storage[$array_one[$i]] = $array_two[$i];
}