PHP Language Array Example Summary and introduction _php tutorial

Source: Internet
Author: User
Today suddenly encountered the problem of PHP, found this article, very old article, but very practical, next I will tidy up the updated php5 array
PHP4.0 has more than 30 new array-related functions. Many of these general functions allow you to check for specific objects in a given array, count arrays of elements, add or delete elements, or sort elements.
If you have a large array, and all you have to do is find a given value that exists, you can use In_array () to return True or false. The following code will output "not found the This array"-because you will find a "Alber" in $namesarray that does not exist.
$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 on this array!";
}
?>
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:
$namesArray = Array ("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
$count = count ($namesArray);?>
The $count value will be 7.
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. When merging, each array is arranged in the order in which it is needed. If your array already has an internal sort, you need to reorder the new merged array.
Let's start with adding elements to the end of an existing array, using the function Array_push ():
/* Create original array */
$fruitArray = Array ("Apple", "orange", "banana", "kiwi", "pear");
/* Add to original array */
Array_push ($fruitArray, "Grape", "pineapple", "tomato");
/* List each element by its key value */
while (list ($key, $value) = each ($fruitArray)) {
echo "$key: $value
";
}
?>
This will show:
0:apple
1:orange
2:banana
3:kiwi
4:pear
5:grape
6:pineapple
7:tomato
When you need to add elements to the beginning of an array, the code is very similar. The difference is just the function name: Array_unshift () instead of Array_push ().
/* Create original array */
$fruitArray = Array ("Apple", "orange", "banana", "kiwi", "pear");
/* Add to original array */
Array_unshift ($fruitArray, "Grape", "pineapple", "tomato");
/* List each element by its key value */
while (list ($key, $value) = each ($fruitArray)) {
echo "$key: $value
";
}
?>
This will show:
0:grape
1:pineapple
2:tomato
3:apple
4:orange
5:banana
6:kiwi
7:pear
The function Array_merge () merges two or more arrays.
/* Create original array */
$fruitArray = Array ("Apple", "orange", "banana", "kiwi", "pear");
$vegArray = Array ("carrot", "green beans", "asparagus", "artichoke", "corn");
/* Merge as an array */
$goodfoodArray = Array_merge ($fruitArray, $vegArray);
/* List each element by its key value */
while (list ($key, $value) = each ($goodfoodArray)) {
echo "$key: $value
";
}
?>
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 you have added elements and merges to an array, practice removing the element functions. You can use the function Array_pop () to remove an element from the end of an array. If the function Array_shift () is used, an element is removed from the beginning of an array. In fact, when you delete an element from an array, this element is still available to you-when you pop or shift an element from an existing array.
Use the Array_pop () function to remove a value from the end of the array:
/* Create an array */
$fruitArray = Array ("Apple", "orange", "banana", "kiwi", "pear");
/* Eject a value at the end */
$popped = Array_pop ($fruitArray);
/* Lists the new array contents, as well as the popup values */
while (list ($key, $value) = each ($fruitArray)) {
echo "$key: $value
";
}
echo "
And finally, in $popped: $popped ";
?>
This will show:
0:apple
1:orange
2:banana
3:kiwi
And finally, in $popped: Pear
Next, delete an element from the end of an array:?????????????????
Below, remove a value from the end of the array:
/* Create an array */
$fruitArray = Array ("Apple", "orange", "banana", "kiwi", "pear");
/* Remove a value from the head of the array */
$shifted = Array_shift ($fruitArray);
/* Lists the contents of the new array and the values to be removed */
while (list ($key, $value) = each ($fruitArray)) {
echo "$key: $value
";
}
echo "
And finally, in $shifted: $shifted ";
?>
This will show:
0:orange
1:banana
2:kiwi
3:pear
And finally, in $shifted: Apple
There are many functions that can help you sort the array elements. But I'll show you the basic sort to help you understand the process:
/* Create original array */
$fruitArray = Array ("Apple", "orange", "banana", "kiwi", "pear");
/* Sort */
Sort ($fruitArray);
/* Reset it to display the array correctly from beginning to end */
/* List each element by its key value */
while (list ($key, $value) = each ($fruitArray)) {
echo "$key: $value
";
}
?>
This will show:
0:apple
1:banana
2:kiwi
3:orange
4:pear

http://www.bkjia.com/PHPjc/364255.html www.bkjia.com true http://www.bkjia.com/PHPjc/364255.html techarticle today suddenly encountered the problem of PHP, found this article, very old article, but very practical, next I will tidy up the updated php5 array PHP4.0 The CCP has more than 30 new array related letters ...

  • 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.