PHP array functions play a role in our Encoding

Source: Internet
Author: User

If there is a large array and you need to find out whether it contains a specific element, you can use the PHP Array Function in_array (). The following example shows "Not found in this array", because Albert is searched in an array named $ namesArray, and such an element does Not exist in the $ namesArray array.

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

If You change the value of $ lookingFor to Mary, You will get "You 've got it !" Because Mary is an element in the $ namesArray array.

To count the number of elements in an array, simply use the PHP Array Function count:

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

The returned $ count value is 7.

You can add elements at the beginning or end of an array, or use the PHP Array Function array_merge () to create a new array containing two or more elements in the array, the order of elements is arranged in the specified order. If the original array is out of order, it needs to be re-ordered after merging.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.