更改数组PHP的键

时间:2014-04-18 08:31:02

标签: php arrays

我有这个数组$theme_name

Array
(
    [0] => template0
    [1] => template1
    [2] => template2
    [3] => template3
    [4] => template4
    [5] => template5
    [6] => template6
)

和另一个具有相同长度$theme_info

的数组
Array
(
    [0] => my template n 00
    [1] => my template n 01
    [2] => my template n 02
    [3] => my template n 03
    [4] => my template n 04
    [5] => my template n 05
    [6] => my template n 06
)

基本上我想要的是拥有这个数组:

Array
(
    [template0] => my template n 00
    [template1] => my template n 01
    [template2] => my template n 02
    [template3] => my template n 03
    [template4] => my template n 04
    [template5] => my template n 05
    [template6] => my template n 06
)

为什么这不起作用?

foreach ($themes_info as $key => $value) {
    include($value['directory']) ;
    $theme_info[] = $info;
    $theme_name[] = $value['name'];
}


 foreach ($theme_name as $key => $value) {
    $value = $theme_info[$key];
}

FYI $themes_info拥有名称和目录安置的所有主题。

2 个答案:

答案 0 :(得分:3)

使用array_combine()

$result = array_combine($theme_name, $theme_info);

Demo

答案 1 :(得分:2)

您可以在PHP中轻松完成此操作。就像这样:

for ($i = 0; $i < count($themes_info); $i++) {
   include($value['directory']);
   $theme_info[] = $info;
   $theme_name["someKey$i"] = $value['name'];
}

将someKey替换为该阵列的首选键。或者对于任何其他阵列。