PHP数组array类常见操作示例

本文实例讲述了PHP数组array类常见操作,分享给大家供大家参考,具体如下:

array_merge($arr1,$arr2....);//合并一个或多个数组,例:

  1. <?php
  2. $beginning = 'foo';
  3. $end = array(1 => 'bar');
  4. $result = array_merge((array)$beginning, (array)$end);
  5. print_r($result);
  6. ?>

以上例程会输出:

  1. Array
  2. (
  3. [0] => foo
  4. [1] => bar
  5. )

2、示例:

  1. <?php
  2. $array1 = array("color" => "red", 2, 4);
  3. $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
  4. $result = array_merge($array1, $array2);
  5. print_r($result);
  6. ?>

以上例程会输出:

  1. Array
  2. (
  3. [color] => green
  4. [0] => 2
  5. [1] => 4
  6. [2] => a
  7. [3] => b
  8. [shape] => trapezoid
  9. [4] => 4
  10. )

3、示例:

  1. <?php
  2. $array1 = array();
  3. $array2 = array(1 => "data");
  4. $result = array_merge($array1, $array2);
  5. ?>

别忘了数字键名将会被重新编号!

  1. Array
  2. (
  3. [0] => data
  4. )

4、示例:

  1. <?php
  2. $array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
  3. $array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
  4. $result = $array1 + $array2;
  5. var_dump($result);
  6. ?>

运行结果:

  1. array(5) {
  2. [0]=>
  3. string(6) "zero_a"
  4. [2]=>
  5. string(5) "two_a"
  6. [3]=>
  7. string(7) "three_a"
  8. [1]=>
  9. string(5) "one_b"
  10. [4]=>
  11. string(6) "four_b"
  12. }

array_diff($arr1,$arr2);//计算数组的差集(对比返回在 array1 中但是不在 array2 及任何其它参数数组中的值。)

例:

  1. <?php
  2. $array1 = array("a" => "green", "red", "blue", "red");
  3. $array2 = array("b" => "green", "yellow", "red");
  4. $result = array_diff($array1, $array2);
  5. print_r($result);
  6. ?>

在 $array1 中多次出现的值一样处理,输出结果为:

  1. Array
  2. (
  3. [1] => blue
  4. )

array_diff_assoc($arr1,$arr2...);//带索引检查计算数组的差集

array_diff_assoc() 返回一个数组,该数组包括了所有在 array1 中但是不在任何其它参数数组中的值

  1. <?php
  2. $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
  3. $array2 = array("a" => "green", "yellow", "red");
  4. $result = array_diff_assoc($array1, $array2);
  5. print_r($result);
  6. ?>

以上例程会输出:

  1. Array
  2. (
  3. [b] => brown
  4. [c] => blue
  5. [0] => red
  6. )

array_keys($array);//返回数组中部分的或所有的键名(返回 input 数组中的数字或者字符串的键名。)

  1. <?php
  2. $array = array(0 => 100, "color" => "red");
  3. print_r(array_keys($array));
  4. $array = array("blue", "red", "green", "blue", "blue");
  5. print_r(array_keys($array, "blue"));
  6. $array = array("color" => array("blue", "red", "green"),
  7. "size" => array("small", "medium", "large"));
  8. print_r(array_keys($array));
  9. ?>

以上例程会输出:

  1. Array
  2. (
  3. [0] => 0
  4. [1] => color
  5. )
  6. Array
  7. (
  8. [0] => 0
  9. [1] => 3
  10. [2] => 4
  11. )
  12. Array
  13. (
  14. [0] => color
  15. [1] => size
  16. )

array_slice($array, $offset, $length);//从数组中取出一段

参数:array::输入的数组, offset:序列偏移量, length:单元长度

  1. $input = array("a", "b", "c", "d", "e");
  2. $output = array_slice($input, 2); // returns "c", "d", and "e"
  3. $output = array_slice($input, -2, 1); // returns "d"
  4. $output = array_slice($input, 0, 3); // returns "a", "b", and "c"