随时随地进行数组合并
Array merge on the go
我经常开始定义一个 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',
));
是否有一种 better/cleaner/faster 方法可以随时随地合并数组?
我认为您不需要这样复杂化。这也行。不过绝对是一个较短的版本:
<?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);
老实说,这已经很干净了,但是如果你想让代码更干净,看看是什么反复引起了困难。像这样定义一个辅助函数:
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',
));
最简单的方法似乎是:
$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
The + operator returns the right-hand array appended to the left-hand
array; for keys that exist in both arrays, the elements from the
left-hand array will be used, and the matching elements from the
right-hand array will be ignored.
我经常开始定义一个 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',
));
是否有一种 better/cleaner/faster 方法可以随时随地合并数组?
我认为您不需要这样复杂化。这也行。不过绝对是一个较短的版本:
<?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);
老实说,这已经很干净了,但是如果你想让代码更干净,看看是什么反复引起了困难。像这样定义一个辅助函数:
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',
));
最简单的方法似乎是:
$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
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.