阵列合并在旅途中

时间:2017-12-15 10:20:07

标签: php arrays array-merge

我经常开始定义一个array,然后弄清楚我需要将它分成两部分或更多部分。然后,我将以$array('key' => 'value')开头,获取我的第一个值,然后必须重写数组其他部分的代码,如下所示:$array['key'] = 'value'。但这很麻烦。

所以我尝试了以下内容,这似乎有效:

$my_true_love_sent_me_for_christmas = array(
    'First day'     =>  'A Partridge in a Pear Tree',
    'Second day'    =>  '2 Turtle Doves',
    'Third day'     =>  '3 French Hens',
    'Fourth day'    =>  '4 Calling Birds',
);

breathe(); // breathe, and then I want to continue my array where I left it.

$my_true_love_sent_me_for_christmas = array_merge($my_true_love_sent_me_for_christmas, array(
    'Fifth day'     =>  '5 Golden Rings',
    'Sixth day'     =>  '6 Geese a Laying',
    'Seventh day'   =>  '7 Swans a Swimming',
    'Eighth day'    =>  '8 Maids a Milking',
));

breathe(); // breathe, and then I want to continue my array where I left it.

$my_true_love_sent_me_for_christmas = array_merge($my_true_love_sent_me_for_christmas, array(
    'Ninth day'     =>  '9 Ladies Dancing',
    'Tenth day'     =>  '10 Lords a Leaping',
    'Eleventh day'  =>  '11 Pipers Piping',
    'Twelfth day'   =>  '12 Drummers Drumming',
));

是否有更好/更清洁/更快的方式在移动中合并阵列?

3 个答案:

答案 0 :(得分:2)

最简单的方法似乎是:

$foo = [
    'First day'  => 'A Partridge in a Pear Tree',
    'Second day' => '2 Turtle Doves',
    'Third day'  => '3 French Hens',
    'Fourth day' => '4 Calling Birds',
];

$foo += [
    'Fifth day'   => '5 Golden Rings',
    'Sixth day'   => '6 Geese a Laying',
    'Seventh day' => '7 Swans a Swimming',
    'Eighth day'  => '8 Maids a Milking',
];

$foo += [
    'Ninth day'    => '9 Ladies Dancing',
    'Tenth day'    => '10 Lords a Leaping',
    'Eleventh day' => '11 Pipers Piping',
    'Twelfth day'  => '12 Drummers Drumming',
];

print_r($foo);

参考:https://php.net/manual/language.operators.array.php

  

+运算符返回附加到左侧的右侧数组   阵列;对于存在于两个数组中的键,来自的数组   将使用左手数组,以及来自的匹配元素   右手阵列将被忽略。

答案 1 :(得分:1)

在我的诚实意见中,这已经很干净了,但是如果你想让代码更清洁,那么看看反复造成困难的是什么。定义一个这样的辅助函数:

function addRange(&$mainArray, $range) {
    foreach ($range as $k => $v) {
        $mainArray[$k] = $v;
    }
}

然后你可以稍微简化你的代码:

$my_true_love_sent_me_for_christmas = array(
    'First day'     =>  'A Partridge in a Pear Tree',
    'Second day'    =>  '2 Turtle Doves',
    'Third day'     =>  '3 French Hens',
    'Fourth day'    =>  '4 Calling Birds',
);

breathe(); // breathe, and then I want to continue my array where I left it.

addRange($my_true_love_sent_me_for_christmas, array(
    'Fifth day'     =>  '5 Golden Rings',
    'Sixth day'     =>  '6 Geese a Laying',
    'Seventh day'   =>  '7 Swans a Swimming',
    'Eighth day'    =>  '8 Maids a Milking',
));

breathe(); // breathe, and then I want to continue my array where I left it.

addRange($my_true_love_sent_me_for_christmas, array(
    'Ninth day'     =>  '9 Ladies Dancing',
    'Tenth day'     =>  '10 Lords a Leaping',
    'Eleventh day'  =>  '11 Pipers Piping',
    'Twelfth day'   =>  '12 Drummers Drumming',
));

答案 2 :(得分:0)

我认为你不需要这样复杂化。这也有效。绝对是一个较短的版本:

<?php
$my_true_love_sent_me_for_christmas = array(
    'First day'     =>  'A Partridge in a Pear Tree',
    'Second day'    =>  '2 Turtle Doves',
    'Third day'     =>  '3 French Hens',
    'Fourth day'    =>  '4 Calling Birds',
);

breathe(); // breathe, and then I want to continue my array where I left it.

$my_true_love_sent_me_for_christmas['Fifth day']     =  '5 Golden Rings';
$my_true_love_sent_me_for_christmas['Sixth day']     =  '6 Geese a Laying';
$my_true_love_sent_me_for_christmas['Seventh day']   =  '7 Swans a Swimming';
$my_true_love_sent_me_for_christmas['Eighth day']    =  '8 Maids a Milking';

breathe(); // breathe, and then I want to continue my array where I left it.

$my_true_love_sent_me_for_christmas['Ninth day']     =  '9 Ladies Dancing';
$my_true_love_sent_me_for_christmas['Tenth day']     =  '10 Lords a Leaping';
$my_true_love_sent_me_for_christmas['Eleventh day']  =  '11 Pipers Piping';
$my_true_love_sent_me_for_christmas['Twelfth day']   =  '12 Drummers Drumming';

比上述更好的版本是在某处定义所有数组,然后一次使用array_merge

$my_true_love_sent_me_for_christmas = $chrismas1;

breathe(); // breathe, and then I want to continue my array where I left it.

$my_true_love_sent_me_for_christmas = array_merge($my_true_love_sent_me_for_christmas, $chrismas2);

breathe(); // breathe, and then I want to continue my array where I left it.

$my_true_love_sent_me_for_christmas = array_merge($my_true_love_sent_me_for_christmas, $chrismas3);