array_flip(array);數組
//傳遞一個數組參數,對該數組的鍵、值進行翻轉spa
例如:code
$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...])blog
//對兩個或多個數組進行合併(傳遞一個數組參數也是能夠的)索引
須要注意的是:ip
幾個例子:class
//沒有指定索引 $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+array2di
這種寫法其實沒用過,研究了一把,他其實也是用來合併數組操做。用法和array_merge有必定的區別。co
幾個例子:
//沒有指定索引 $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 )