HTML&PHP學習筆記(4)

來源:互聯網
上載者:User

標籤:style   blog   ar   color   使用   sp   for   div   log   

建立數組:

$products = array(‘a‘,‘b‘,‘c‘);

range(1,10);  //1-10的數字數組

 

訪問數組內容:

$products[0];$products[3] = ‘d‘;    //增加元素

逐一查看:

for ($i = 0; $i < 3; $i++){    echo $products[$i]." ";}foreach ($products as $current){    echo $current. " ";}

 

關聯索引:

$prices = array(‘a‘=>100, ‘b‘=>10, ‘c‘=>4);

迴圈:

foreach ($prices as $key => $value) {    echo $key." - ".$value."<br />";}

or

while ($element = each($price)){    echo $element[‘key‘];    echo " - ";    echo $element[‘value‘];    echo "<br />";}

or

while (list($product, $price) = each($prices)){    echo "$product - $price<br />";}

each() 將記錄當前元素,使用reset()重設到開始處。

 

多維陣列:

$products = array( array(‘a‘,‘a1‘,11), array(‘b‘,‘b2‘,12), array(‘c‘,‘c3‘,13));

數組排序:

sort()  //字母順序,大寫在小寫前面

asort()  //關聯陣列,用元素值排序

ksort()  //關聯陣列,用關鍵字排序

反向排序:

rsort()  arsort()  krsort()

 

對數組進行重新排序:

shuffle()  //隨機

array_reverse()  //逆序

 

explode(separator, string);    //分割字串

 

在數組中瀏覽:

each()    //返回當前元素,指標前移next()    //指標前移,返回新元素
current()  //返回數組中的當前元素
reset()  //返回第一個元素
end()    //返回最後一個元素
prev()    // 指標後移,返回新元素

對數組的每一個元素應用任何函數:

array_walk(array, func, [userdata])   //自訂函數,userdata作為參數傳遞給這個函數

 

統計數組元素個數:

count()

sizeof()    //同上

array_count_values()  //返回一個包含頻率表的關聯陣列

 

將數群組轉換成標量變數:

extract()

HTML&PHP學習筆記(4)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.