The function of PHP array function in our coding

Source: Internet
Author: User
Tags array arrays contains count functions

The PHP array function is a very powerful collection of functions. We can sort, delete, and merge the arrays by using these functions. Today we will introduce you to the PHP array function in the actual application of what will play a role. Hope to help the beginners.

If you have a large array and you need to find out if it contains a particular element, you can use the PHP array function In_array (). The following example displays "not found in this array" because it finds Albert in an array named $namesarray, and there is no such element in the $namesarray array.

If you have a large array and you need to find out if it contains a particular element, you can use the PHP array function In_array (). The following example displays "not found in this array" because it finds 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 in this array!";
    7. }
    8. ?>

If you change the value of $lookingfor to Mary, you will get "you ' ve found it!" , 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 ("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
    2. $countcount = count ($namesArray);?>

The value of the returned $count is 7.

You can add elements at the beginning or end of an array, and you can use the PHP array function array_merge () to create a new array of elements in two or more arrays, and when merging, the order of the elements is arranged in the order specified, if the original array is ordered, You need to reorder it 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.