标签:style blog ar color 使用 sp strong on div
array_flip(array);
//传递一个数组参数,对该数组的键、值进行翻转
例如:
$a = array( ‘a‘, ‘b‘, ‘c‘ ); print_r(array_flip($a)); //输出为: Array ( [a] => 0 [b] => 1 [c] => 2 ) //需要注意的是: array_flip(): Can only flip STRING and INTEGER values
array_merge (array1,array2[,aray3...])
//对两个或多个数组进行合并(传递一个数组参数也是可以的)
需要注意的是:
几个例子:
//没有指定索引 $a = array( ‘a‘, ‘b‘, ); $b = array( ‘a‘, ‘b‘, ); $arr = array_merge($a,$b); //输出: Array ( [0] => a [1] => b [2] => a [3] => b ) //======================= //显式指定数字键名 $a = array( 3=>‘a‘, 4=>‘b‘, ); $b = array( 1=>‘a‘, 2=>‘b‘, ); $arr = array_merge($a,$b); //输出: Array ( [0] => a [1] => b [2] => a [3] => b ) //======================= //显式指定数字键名和无索引混合 $a = array( 3=>‘a‘, ‘b‘, ); $b = array( ‘a‘, 2=>‘b‘, ); $arr = array_merge($a,$b); //输出: Array ( [0] => a [1] => b [2] => a [3] => b ) //======================= //显式指定非数字索引1 $a = array( ‘a‘=>1, ‘b‘, ); $b = array( ‘a‘, ‘b‘, ); $arr = array_merge($a,$b); //输出: Array ( [a] => 1 [0] => b [1] => a [2] => b ) //======================= //显式指定非数字索引2 $a = array( ‘a‘=>1, ‘b‘=>2, ); $b = array( ‘a‘, ‘b‘=>22, ); $arr = array_merge($a,$b); //输出: Array ( [a] => 1 [b] => 22 [0] => a )
其他的几种组合例子就不贴上去了……
array1+array2
这种写法其实没用过,研究了一把,他其实也是用来合并数组操作。用法和array_merge有一定的区别。
几个例子:
//没有指定索引 $a = array( ‘a‘, ‘b‘, ); $b = array( ‘c‘, ‘d‘, ‘e‘ ); $arr = $a + $b; //输出: Array ( [0] => a [1] => b [2] => e ) //======================= //指定数字索引 $a = array( 1=>‘a‘, 2=>‘b‘, ); $b = array( 4=>‘c‘, 7=>‘d‘ ); $arr = $a + $b; //输出: Array ( [1] => a [2] => b [4] => c [7] => d ) //======================= //指定索引,存在重复 $a = array( ‘a‘=>11, ‘b‘=>22, ); $b = array( ‘c‘=>33, ‘d‘=>44, ‘a‘=>55 ); $arr = $a + $b; //输出: Array ( [a] => 11 [b] => 22 [c] => 33 [d] => 44 ) //======================= //混合指定索引和无索引1 $a = array( ‘a‘, 2=>‘b‘, ); $b = array( ‘c‘, ‘d‘, 10=>‘a‘, 4=>22 ); $arr = $a + $b; //输出: Array ( [0] => a [2] => b [1] => d [10] => a [4] => 22 ) //======================= //混合指定索引和无索引2 $a = array( 2=>‘b‘, ‘a‘, ); $b = array( ‘c‘, ‘d‘, 10=>‘a‘, 4=>22 ); $arr = $a + $b; //输出: Array ( [2] => b [3] => a [0] => c [1] => d [10] => a [4] => 22 )
array_flip和array+array的搭配使用(去掉两个数组中重复的值):
$a = array( ‘a‘, ‘b‘, ‘c‘ ); $b = array( ‘a1‘, ‘b1‘, ‘c‘ ); $arr = array_flip($a) + array_flip($b); print_r(array_keys($arr)); //输出: Array ( [0] => a [1] => b [2] => c [3] => a1 [4] => b1 )
PHP array_flip() array_merge() array+array的使用总结
标签:style blog ar color 使用 sp strong on div
原文地址:http://www.cnblogs.com/fanchangfa/p/4155274.html