Array PHP4.0 has more than 30 new array-related functions. Many of these common functions allow you to check whether a given array has a specific object, an array element count, an addition or deletion element, or a sort of element.
If you have a large array, and you want to do only to find a given value that exists, you can use In_array () to return True or false. The following code prints "not found in"-because you will be looking for a "alber" that does not exist in $namesarray.
If you change the value of $lookingfor and turn it into "Mary," you will get the message "you ' ve found it!" --because "Mary" is part of the $namesarray.
If you want to count the array elements, you can use the count () function:
You can add elements to any array, either at the beginning or the end of an existing array. You can also use a function to create a new array that contains two or more arrays of elements. Each array is arranged in the order you want it to be merged. If your array already has an internal sort, you need to reorder the new merged arrays.
Let's start with adding elements to the end of an existing array, using the function Array_push ():
while (the list ($key, $value) = each ($fruitArray)) {
echo "$key: $value <br>";
}
?>
This will show:
0:apple
1:orange
2:banana
3:kiwi
4:pear
5:grape
6:pineapple
7:tomato
When you need to add an element to the beginning of an array, the code is very similar. The difference is just the function name: Array_unshift () instead of Array_push ().
while (the list ($key, $value) = each ($goodfoodArray)) {
echo "$key: $value <br>";
}
?>
This will show:
0:apple
1:orange
2:banana
3:kiwi
4:pear
5:carrot
6:green Beans
7:asparagus
8:artichoke
9:corn
Now that we've added elements and merges to the array, we're going to practice deleting the element functions. You can use the function Array_pop () to delete an element from the end of an array. If you use the function Array_shift (), you delete an element from the beginning of an array. And actually when you delete an element from an array, this element is still available to you-when you pop or shift elements from an existing array.
Use the Array_pop () function to remove a value from the end of the array:
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.