我试图使用array_combine函数来组合一组键,这些键是一组数组的字符串。数组合并导致新数组被重新索引。
$keys = array("00","11","22");
$values = array(0 => array("cake", "sprinkles"), 1 => array("dog", "cat"), 2 => array("water","fire"));
$combined_array = array_combine($keys, $values);
结果
Array
(
[0] => Array
(
[0] => cake
[1] => sprinkles
)
[1] => Array
(
[0] => dog
[1] => cat
)
[2] => Array
(
[0] => water
[1] => fire
)
)
期望的结果
Array
(
["00"] => Array
(
[0] => cake
[1] => sprinkles
)
["11"] => Array
(
[0] => dog
[1] => cat
)
["22"] => Array
(
[0] => water
[1] => fire
)
)
答案 0 :(得分:0)
如果这个方法不起作用,你可以尝试其他方法,我没有尝试你的代码,但这是其他解决方案之一:
<?php
$keys = array("00","11","22");
$values = array(0 => array("cake", "sprinkles"), 1 => array("dog", "cat"), 2 => array("water","fire"));
foreach ($keys as $key) {
foreach ($values as $k => $value) {
$result[$key] = $value;
// if you want to unset the initaial array
// unset($values[$k]);
}
}
var_dump($result); exit;
?>
希望它有帮助.. 干杯
答案 1 :(得分:-1)
您的代码很好
仅错字错误$Keys
而不是$keys
$combined_array = array_combine($keys, $values);
echo "<pre>";print_r($combined_array);
输出
Array
(
[00] => Array
(
[0] => cake
[1] => sprinkles
)
[11] => Array
(
[0] => dog
[1] => cat
)
[22] => Array
(
[0] => water
[1] => fire
)
)
另外评论如何通过此array_combine
error
时的输出结果
警告:array_combine()期望参数1为数组,给定
为null