PHP 合并或连接数组
示例
$fruit1 = ['apples', 'pears']; $fruit2 = ['bananas', 'oranges']; $all_of_fruits = array_merge($fruit1, $fruit2); // now value of $all_of_fruits is [0 => 'apples', 1 => 'pears', 2 => 'bananas', 3 => 'oranges']
请注意,这array_merge将更改数字索引,但会覆盖字符串索引
$fruit1 = ['one' => 'apples', 'two' => 'pears']; $fruit2 = ['one' => 'bananas', 'two' => 'oranges']; $all_of_fruits = array_merge($fruit1, $fruit2); // now value of $all_of_fruits is ['one' => 'bananas', 'two' => 'oranges']
array_merge如果无法对索引重新编号,则用第二个数组的值覆盖第一个数组的值。
您可以使用+运算符合并两个数组,以使第一个数组的值永远不会被覆盖,但是它不会对数字索引重新编号,因此,您将丢失具有也在第一个数组中使用的索引的数组的值。
$fruit1 = ['one' => 'apples', 'two' => 'pears']; $fruit2 = ['one' => 'bananas', 'two' => 'oranges']; $all_of_fruits = $fruit1 + $fruit2; // now value of $all_of_fruits is ['one' => 'apples', 'two' => 'pears'] $fruit1 = ['apples', 'pears']; $fruit2 = ['bananas', 'oranges']; $all_of_fruits = $fruit1 + $fruit2; // now value of $all_of_fruits is [0 => 'apples', 1 => 'pears']
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短