user3350731
user3350731

Reputation: 972

Change key of an array PHP

I have this array $theme_name

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

and this other array that has the same lenght $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
)

Basically what I want is to have this array :

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
)

Why this won't work ?

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 have all the themes with names and directories emplacement.

Upvotes: 0

Views: 48

Answers (2)

Amal Murali
Amal Murali

Reputation: 76666

Use array_combine():

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

Demo

Upvotes: 3

Alex van den Hoogen
Alex van den Hoogen

Reputation: 754

You can do this far easier in PHP. Just like this:

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

And replace someKey with your prefered key for that array. Or for any other array for that matter.

Upvotes: 2

Related Questions