PHP合并两个数组的两种方式

WBOY
Lepaskan: 2016-07-25 08:54:37
asal
1128 orang telah melayarinya
  1. echo "\r\n第一种情况\r\n";

  2. $a=array(1,2,3,4,5,6);
  3. $b=array(7,8,9);
  4. $c=array_merge ($a,$b);
  5. print_r($c);
  6. $c=$a+$b;
  7. print_r($c);
  8. $c=$b+$a;
  9. print_r($c);
  10. echo "\r\n第二种情况\r\n";

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

输出结果: 第一种情况 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) Array ( [0] => 7 [1] => 8 [2] => 9 [3] => 4 [4] => 5 [5] => 6 ) 第二种情况 Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => a [7] => x [8] => y ) Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f ) Array ( [0] => a [1] => x [2] => y [3] => d [4] => e [5] => f ) 第三种情况 Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => a [7] => x [8] => y ) Array ( [1] => a [2] => b [3] => c [4] => d [5] => e [6] => f [7] => x [8] => y ) Array ( [1] => a [7] => x [8] => y [2] => b [3] => c [4] => d [5] => e [6] => f )



sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan