• php一维数组合并


    1. $arr1=['1','2','3'];
    2. $arr2=['4',24,'6'];
    3. $all=array_combine($arr1, $arr2);//数组合并函数
    4. var_dump($all);
    1. php中数组的几种合并方法:
    2. array_merge(array1,array2):两个数组中的一维数据合并,二维数组如果key相同,array2中的值会替代array1中的值;
    3. 加号:array1+array2:把array2中key对应在array1不存在的key的值添加到array1中(像当于array1你有4个位置,则那4个位置的值不变,array2有5个位置,只把第5个位置的值加到array1中)
    4. array_merge_recursive:与array_merge唯一不同的是,二维数据的值不会替换,是递归合并到一起;
    5. $array1 = array(2,4,"color" => "red");
    6. $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
    7. $result = array_merge($array1, $array2);
    8. echo "----------------array_merge---------------".PHP_EOL;
    9. print_r($result);
    10. echo "----------------+++++++++++---------------".PHP_EOL;
    11. print_r($array1+$array2);
    12. echo "----------------array_merge_recursive---------------".PHP_EOL;
    13. print_r(array_merge_recursive($array1,$array2));
    14. ----------------array_merge---------------
    15. Array
    16. (
    17. [0] => 2
    18. [1] => 4
    19. [color] => green
    20. [2] => a
    21. [3] => b
    22. [shape] => trapezoid
    23. [4] => 4
    24. )
    25. ----------------+++++++++++---------------
    26. Array
    27. (
    28. [0] => 2
    29. [1] => 4
    30. [color] => red
    31. [shape] => trapezoid
    32. [2] => 4
    33. )
    34. ----------------array_merge_recursive---------------
    35. Array
    36. (
    37. [0] => 2
    38. [1] => 4
    39. [color] => Array
    40. (
    41. [0] => red
    42. [1] => green
    43. )
    44. [2] => a
    45. [3] => b
    46. [shape] => trapezoid
    47. [4] => 4
    48. )
    49. $array1 = array('a','b');
    50. $array2 = array(1,2);
    51. $result = array_merge($array1, $array2);
    52. echo "----------------array_merge---------------".PHP_EOL;
    53. print_r($result);
    54. echo "----------------+++++++++++---------------".PHP_EOL;
    55. print_r($array1+$array2);
    56. echo "----------------array_merge_recursive---------------".PHP_EOL;
    57. print_r(array_merge_recursive($array1,$array2));
    58. ----------------array_merge---------------
    59. Array
    60. (
    61. [0] => a
    62. [1] => b
    63. [2] => 1
    64. [3] => 2
    65. )
    66. ----------------+++++++++++---------------
    67. Array
    68. (
    69. [0] => a
    70. [1] => b
    71. )
    72. ----------------array_merge_recursive---------------
    73. Array
    74. (
    75. [0] => a
    76. [1] => b
    77. [2] => 1
    78. [3] => 2
    79. )

  • 相关阅读:
    Linux 网络操作命令FTP
    01 创建前端项目【小白入门SpringBoot + Vue3】
    Java代码一行怎么运转起来?
    蓝桥等考Python组别六级003
    图计算发展简史(3)
    国际项目管理师PMP证书,值得考嘛?
    XSS攻击
    Excel永远不会消亡!
    SpringCloud gateway+zookeeper实现网关路由
    Python-自动化绘制股票价格通道线
  • 原文地址:https://blog.csdn.net/qq_34631220/article/details/126116372