The role of PHP array functions in our coding embodies _php tutorial

Source: Internet
Author: User
If you have a large array, and you need to find out if it contains a specific element, you can use the PHP array function In_array (). The following example shows "not found in this array" because it looks for Albert in an array named $namesarray, and there is no such element in the $namesarray array.

 
 
  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 on this array!";
  7. }
  8. ?>

If you change the value of $lookingfor to Mary, you will get "you ve found it!" Information because Mary is an element in the $namesarray array.

If you want to count the number of elements in an array, simply use the PHP Array function count () to:

 
  
  
  1. $ Namesarray Array
  2. $ Count Count ?>

The value of the returned $count is 7.

You can add elements at the beginning or end of an array, or you can use the PHP array function array_merge () to create a new array containing two or more elements in the array, and when merging, the order of the elements will be arranged in the order specified, if the original array is ordered, It needs to be reordered after merging.


http://www.bkjia.com/phpjc/446331. HTML www.bkjia.com true http://www.bkjia.com/phpjc/446331.html techarticle If there is a large array, And you need to find out if it contains a specific element, you can use the PHP array function In_array (). The following example shows "not found in this ...

  • Related Article

    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.