Check whether the php array contains specified characters and array Length

Source: Internet
Author: User

This tutorial only describes two parts. One is to use the Array Function in_array to check whether a specified string exists in the array, and use the count function to determine the array length. Definition and usage

This tutorial only describes two parts. One is to use the Array Function in_array to check whether a specified string exists in the array, and use the count function to determine the array length.
Definition and usage
The in_array () function searches for the given value in the array.

Syntax
In_array (value, array, type)
*/

$ OS _list = array ("mac", "nt", "irix", "linux"); // defines an array
If (in_array ("irix", $ OS _list) // check whether the array contains lrix
{
Echo "the current operating system list contains lrix"; // If yes, the output content
}
Echo"
";
If (! In_array ("mac", $ OS _list ))
{
Echo "mac does not exist in the current operating system list"; // case sensitive, so this sentence will not be executed
}


/*
In_array returns true if the given value exists in the array. If the third parameter is set to true, the function returns true only when the element exists in the array and the data type is the same as the given value.

If no parameter is found in the array, the function returns false.

Note: If the value parameter is a string and the type parameter is set to true, the search area is case sensitive.


Definition and usage

Syntax
Count (array, mode)


The count () function calculates the number of units in the array or the number of attributes in the object.

For an array, return the number of its elements. For other values, return 1. If the parameter is a variable but the variable is not defined, 0 is returned. If the mode is set to count_recursive (OR 1), the number of elements in the array in the multi-dimensional array is calculated recursively.

 

// Array Length
*/

$ A [0] = 1; // create an array by assigning values respectively. $ a []
$ A [1] = 3;
$ A [2] = 5;
$ Result = count ($ a); // returns the number of elements in the array $ a 3
Echo $ result;
Echo"
";
$ B [0] = 7;
$ B [5] = 9;
$ B [10] = 11;
$ Result = count ($ B); // returns the number of elements in the array $ B 3
Echo $ result;
Echo"
";
$ Result = count (null); // return 0
Echo $ result;
Echo"
";
$ Result = count (false); // return 1
Echo $ result;


/*
Tips and comments
Note: If the variable is not set or contains an empty array, the function returns 0. You can use the isset () variable to test whether the variable is set.


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.