PHP array_merge合并拆分两个数组

对数组的array_merge函数和+运算符比较迷惑,写了个小程序比较下发现了他们的不同. 特别是+运算符,他的意思是,将右边的数组单元(去重复)追加到左边数组的后面.

实例代码如下:

  1. <?php
  2. $arr1=array("a","b","c");
  3. $arr2=array("c","d","e");
  4. $myarray=array_merge($arr1,$arr2);
  5. print_r($myarray);
  6. $myarray=array_unique($myarray);
  7. print_r($myarray);
  8. ?>

实例代码如下:

  1. <?php
  2. $array1=array(1, 2);//数组1
  3. $array2=array(2, 3);//数组2
  4. $array3=array_merge($array1, $array2);//合并数组;
  5. $array3=array_unique($array3);//移除数组中重复的值
  6. ?>

实例代码如下:

  1. <?php
  2. echo "rn第一种情况rn";
  3. $a=array(1,2,3,4,5,6);
  4. $b=array(7,8,9);
  5. $c=array_merge ($a,$b);
  6. print_r($c);
  7. $c=$a+$b;
  8. print_r($c);
  9. $c=$b+$a;
  10. print_r($c);
  11. echo "rn第二种情况rn";
  12. $a=array('a','b','c','d','e','f');
  13. $b=array('a','x','y');
  14. $c=array_merge ($a,$b);
  15. print_r($c);
  16. $c=$a+$b;
  17. print_r($c);
  18. $c=$b+$a;
  19. print_r($c);
  20. echo "rn第三种情况rn";
  21. $a=array(
  22. 1=>'a',
  23. 2=>'b',
  24. 3=>'c',
  25. 4=>'d',
  26. 5=>'e',
  27. 6=>'f');
  28. $b=array(
  29. 1=>'a',
  30. 7=>'x',
  31. 8=>'y');
  32. $c=array_merge ($a,$b);
  33. print_r($c);
  34. $c=$a+$b;
  35. print_r($c);
  36. $c=$b+$a;
  37. print_r($c);
  38. ?>

结果如下:

  1. 第一种情况
  2. Array
  3. (
  4. [0] => 1
  5. [1] => 2
  6. [2] => 3
  7. [3] => 4
  8. [4] => 5
  9. [5] => 6
  10. [6] => 7
  11. [7] => 8
  12. [8] => 9
  13. )
  14. Array
  15. (
  16. [0] => 1
  17. [1] => 2
  18. [2] => 3
  19. [3] => 4
  20. [4] => 5
  21. [5] => 6
  22. )
  23. Array
  24. (
  25. [0] => 7
  26. [1] => 8
  27. [2] => 9
  28. [3] => 4
  29. [4] => 5
  30. [5] => 6
  31. )
  32. 第二种情况
  33. Array
  34. (
  35. [0] => a
  36. [1] => b
  37. [2] => c
  38. [3] => d
  39. [4] => e
  40. [5] => f
  41. [6] => a
  42. [7] => x
  43. [8] => y
  44. )
  45. Array
  46. (
  47. [0] => a
  48. [1] => b
  49. [2] => c
  50. [3] => d
  51. [4] => e
  52. [5] => f
  53. )
  54. Array
  55. (
  56. [0] => a
  57. [1] => x
  58. [2] => y
  59. [3] => d
  60. [4] => e
  61. [5] => f
  62. )
  63. 第三种情况
  64. Array
  65. (
  66. [0] => a
  67. [1] => b
  68. [2] => c
  69. [3] => d
  70. [4] => e
  71. [5] => f
  72. [6] => a
  73. [7] => x
  74. [8] => y
  75. )
  76. Array
  77. (
  78. [1] => a
  79. [2] => b
  80. [3] => c
  81. [4] => d
  82. [5] => e
  83. [6] => f
  84. [7] => x
  85. [8] => y
  86. )
  87. Array
  88. (
  89. [1] => a
  90. [7] => x
  91. [8] => y
  92. [2] => b
  93. [3] => c
  94. [4] => d
  95. [5] => e
  96. [6] => f
  97. )

拆分数组 array_slice()

array_slice()函数将返回数组中的一部分,从键offset开始,到offset+length位置结束.其形式:

Php代码

1.array array_slice (array array, int offset[,int length])

array array_slice (array array, int offset[,int length])

offset 为正值时,拆分将从距数组开头的offset 位置开始;如果offset 为负值,则拆分从距数组末尾的offset 位置开始.如果省略了可选参数length,则拆分将从offset 开始,一直到数组的最后一个元素.如果给出了length 且为正数,则会在距数组开头的offset+length 位置结束.相反,如果给出了length且为负数,则在距数组开头的count(input_array)-|length|位置结束.考虑一个例子:

Php代码

实例代码如下:

  1. <?php
  2. $fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon");
  3. $subset = array_slice($fruits, 3);
  4. print_r($subset);
  5. // output
  6. // Array ( [0] => Pear [1] => Grape [2] => Lemon [3] => Watermelon )
  7. ?>

然后我们使用下负长度:

Php代码

实例代码如下:

  1. <?php
  2. $fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon");
  3. $subset = array_slice($fruits, 2, -2);
  4. print_r($subset);
  5. // output
  6. // Array ( [0] => Orange [1] => Pear [2] => Grape )
  7. ?>