PHP使用现有数组的新数组分配数组键

To save on typing out the key name for every single array, I want to be able to build lists like..

$lists = array (
0 => array ('A', 'B', 'C', 'D', 'E');
1 => array ('A', 'B', 'C', 'D', 'E');
2 => array ('A', 'B', 'C', 'D', 'E');
)

.. and then assign the same key names to all them (either before or after)..

Key1, Key2, Key3, Key4, Key5

.. so when they're called, I can do something like..

foreach($lists as $list) {
    showList($list);
} 

.. and then within the showList() function, I can call the 5 keys by the key name.

The function I have set no problem, but it's assigning the key names that I'm not sure how to do. Sorry if my terminology isn't accurate, but hopefully I explained it well enough.

array_combine will make an associative array from an array of keys and an array of values.

$keys = array('Key1', 'Key2', 'Key3', 'Key4', 'Key5');
foreach ($lists as $list) {
    showList(array_combine($keys, $list));
}

If you want to modify $lists permanently, you can do:

foreach ($lists as &$list) {
    $list = array_combine($keys, $list);
}

The reference variable &$list makes this replace the elements in place.

Try this :

<?php
$keys = array('Key1', 'Key2', 'Key3', 'Key4', 'Key5');
$lists = array (
                    array ('A', 'B', 'C', 'D', 'E'),
                    array ('A', 'B', 'C', 'D', 'E'),
                    array ('A', 'B', 'C', 'D', 'E')
                );

function showList($list){
    global $keys;
    return array_combine($keys, $list);
}

$output = array_map("showList", $lists);

echo "<pre>";
print_r($output);
?>

Result:

Array
(
    [0] => Array
        (
            [Key1] => A
            [Key2] => B
            [Key3] => C
            [Key4] => D
            [Key5] => E
        )

    [1] => Array
        (
            [Key1] => A
            [Key2] => B
            [Key3] => C
            [Key4] => D
            [Key5] => E
        )

    [2] => Array
        (
            [Key1] => A
            [Key2] => B
            [Key3] => C
            [Key4] => D
            [Key5] => E
        )

)