0

我有两个看起来像这样的数组:

$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'),
);

我知道修复非常简单 - 但我不知道它是什么......

4

3 回答 3

4

这看起来像是可以用array_combine完成的。

于 2013-11-13T20:35:04.280 回答
2
$array_result = array();
foreach ($array_one as $key => $val)
    $array_result[$val] = $array_two[$key];
于 2013-11-13T20:35:26.417 回答
0

我认为你的问题是你的嵌套循环遍历数组二的所有值为什么不尝试一个 for 循环:

for ($i = 0; $i < count($array_one); $i += 1) {
    $some_storage[$array_one[$i]] = $array_two[$i];
}
于 2013-11-13T20:38:50.317 回答