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.
? $namesArray = Array ("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
- $lookingFor = "Albert";
- if (In_array ($lookingFor, $namesArray)) {
- echo "You ' ve found it!";
- } else {
- echo "Not found in this array!";
- }
- ?>
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:
? $namesArray = Array ("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
- $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.