PHP數組函數對我們編碼中的作用體現_PHP教程

來源:互聯網
上載者:User
如果有一個很大的數組,而你需要找出其中是否包含一個特定的元素,就可以使用PHP數組函數in_array()。下面的例子將顯示“Not found in this array”,因為在一個名字為$namesArray的數組中尋找Albert,而在$namesArray數組中不存在這樣一個元素。

 
  1. $namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
  2. $lookingFor = "Albert";
  3. if (in_array($lookingFor, $namesArray)) {
  4. echo "You've found it!";
  5. } else {
  6. echo "Not found in this array!";
  7. }
  8. ?>

如果把$lookingFor的值改為Mary,就會得到“You've found it!”的資訊,因為Mary是$namesArray數組中的一個元素。

如果要對一個數組中的元素個數進行計數,只要簡單地使用PHP數組函數count()即可:

 
  1. $namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
  2. $countcount = count($namesArray); ?>

返回的$count的值為7。

可以在一個數組的開頭或結尾處添加元素,還可以使用PHP數組函數array_merge()來建立一個包含二個或更多數組中元素的新數組,合并時,元素的順序會按指定的順序排列,如果原來的數組是被排過序的,在合并後需要對它重新排序。


http://www.bkjia.com/PHPjc/446331.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446331.htmlTechArticle如果有一個很大的數組,而你需要找出其中是否包含一個特定的元素,就可以使用PHP數組函數in_array()。下面的例子將顯示“Not found in this...

  • 聯繫我們

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