PHP在关联数组中添加一个新的键 - 值对

时间:2015-09-08 09:49:02

标签: php arrays associative-array

嗨,我想请一些帮助。我有这样的数组

$my_array = array(
    array(
        'key_1' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'key_2'     => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'key_3'     => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
    ),
    array(
        'key_1' => 'Integer lorem quam, adipiscing condimentum tristique vel, eleifend sed turpis.',
        'key_2' => 'Integer lorem quam, adipiscing condimentum tristique vel, eleifend sed turpis.',
        'key_3' => 'Integer lorem quam, adipiscing condimentum tristique vel, eleifend sed turpis.',
    ),
    array(
        'key_1' => 'Pellentesque cursus arcu id magna euismod in elementum purus molestie.',
        'key_2' => 'Pellentesque cursus arcu id magna euismod in elementum purus molestie.',
        'key_3' => 'Pellentesque cursus arcu id magna euismod in elementum purus molestie.',
    ),
);

我想要做的是在每个内部数组项上追加key_4 => value_4,所以为了做到这一点,我使用了foreach这样的循环:

foreach ($my_array as $item) {
            $item['key_4'] = file_get_contents($some_file);
            // echo '<pre>'. print_r($item) . '</pre>'; // the new keys seems to appended properly here
}
echo '<pre>'. print_r($my_array) . '</pre>'; // the new keys are not in my array (!!!)

所以我要跳的是这样:

$my_array = array(
        array(
            'key_1' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
            'key_2'     => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
            'key_3'     => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
            'key_4'     => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        ),
        array(
            'key_1' => 'Integer lorem quam, adipiscing condimentum tristique vel, eleifend sed turpis.',
            'key_2' => 'Integer lorem quam, adipiscing condimentum tristique vel, eleifend sed turpis.',
            'key_3' => 'Integer lorem quam, adipiscing condimentum tristique vel, eleifend sed turpis.',
            'key_4' => 'Integer lorem quam, adipiscing condimentum tristique vel, eleifend sed turpis.',
        ),
        array(
            'key_1' => 'Pellentesque cursus arcu id magna euismod in elementum purus molestie.',
            'key_2' => 'Pellentesque cursus arcu id magna euismod in elementum purus molestie.',
            'key_3' => 'Pellentesque cursus arcu id magna euismod in elementum purus molestie.',
            'key_4' => 'Pellentesque cursus arcu id magna euismod in elementum purus molestie.',
        ),
    );

但如果我转储我的阵列(请参阅循环中的print_r)我没有得到新密钥=&gt;配对物品。什么想法我做错了???

2 个答案:

答案 0 :(得分:1)

默认情况下,foreach会创建数组的副本。

要处理原文,请按引用进行迭代:

foreach ($my_array as &$item){ //note & char

或使用for循环

for($i = 0; $i < count($my_array); $i++){
    $my_array[$i]['key_4'] = file_get_contents($some_file);

另外,您可以使用array_map

$my_array = array_map(function($item){
    $item['key_4'] = file_get_contents($some_file);
    return $item;
    }, $my_array);

这将使用修改后的副本覆盖原始数组,因此可能会比其他两个选项更糟糕

答案 1 :(得分:0)

您需要使用主数组的当前key来存储新值。试试这个 -

foreach ($my_array as $key => $item) {
    $my_array[$key]['key_4'] = //new value;
}