php學習7——數組

來源:互聯網
上載者:User

建立數組

$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)//

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.