建立數組
$my_arr=array();
$my_arr=array('a','b','c','d');
$my_arr=array(1,2,3,4);
$my_arr=array('a'=>'x1','b'=>'x2','c'=>'x3','d'=>'x4');
$str=range('a','z');
print_r ($str);
out:Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => g [7] => h [8] => i [9] => j [10] => k [11] => l [12] => m [13] => n [14] => o [15] => p [16] => q [17] => r [18] => s [19] => t [20] => u [21] => v [22] => w [23] => x [24] => y [25] => z )
$str=range('a','z');
print_r ($str);
out:Array ( [0] => a [1] => c [2] => e [3] => g [4] => i [5] => k [6] => m [7] => o [8] => q [9] => s [10] => u [11] => w [12] => y )
$str=range(0,10 ,-2);
print_r ($str);
out:Array ( [0] => 0 [1] => 2 [2] => 4 [3] => 6 [4] => 8 [5] => 10 )
訪問數組
$my_arr[0];
$my_arr['a'];
輸出數組
print_r(array,true)
var_export(array,true)
var_dump(array)
數組輸出為字串(兩個函數是相同的互為別名)
implode(', ',array);
join(', ',array);
ex
$my_arr=array('a','b','c','d');
$str=implode('-',$my_arr);
echo $str;
out:a-b-c-d
$my_arr=array('a'=>'x1','b'=>'x2','c'=>'x3','d'=>'x4');
$str=implode('-',$my_arr);
echo $str;
out:x1-x2-x3-x4
只輸出值或鍵
array_values(array)//把數組中的值輸出為字串
array_keys(array)//把數組中的鍵輸出為字串
數組追加
array_merge($arr,$arr2,……)
$my_arr1=array('a','b','c','d');
$my_arr2=array(1,2,3,4);
$my_arr3=array('a'=>'x1','b'=>'x2','c'=>'x3','d'=>'x4');
$new_arr=array_merge($my_arr1,$my_arr2);
$new_arr2=array_merge($my_arr1,$my_arr2,$my_arr3);
print_r( $new_arr) ;
print_r($new_arr2);
out:Array ( [0] => a [1] => b [2] => c [3] => d [4] => 1 [5] => 2 [6] => 3 [7] => 4 )
Array ( [0] => a [1] => b [2] => c [3] => d [4] => 1 [5] => 2 [6] => 3 [7] => 4 [a] => x1 [b] => x2 [c] => x3 [d] => x4 )
數組元素統計
count()=sizeof();
刪除數組指定的元素,第一個元素和最後一個元素(棧和隊列)
unset($array['key'])
array_shift(array)
array_pop(array)
刪除一個或多個數組元素
array_splice(array,start,length)
array_slice(array,start,length)
尋找
in_array($str,array) 返回bool值,存在返回true,否則返回false
array_key_exists($key,array)
數組處理
max(array),
min(array),
array_sum(array)
回呼函數
array_walk(array,funcname)//會修改原數組
array_map(funcname,array)//不會修改原數組
array_filter(array,funcname)//