Application of PHP4.0 array correlation function

Source: Internet
Author: User
Tags array arrays contains functions pear string sort variable
function | Array (Coolman)

PHP is one of the most popular server-side script languages because of its fast, reliable, cross-platform application and open source code, and today I talk to you about the application of array-related functions in PHP4.0. PHP 4.0 provides an array-related function that is not available in more than 30 PHP 3.0 for programmers to use. These functions are commonly used in the functions are: Check whether an array contains a value, calculate the number of occurrences of a value in the array, add or remove elements of the array and the elements of the array reordering, and so on, I will discuss in these areas.

1. Check the values in the array
If you have a large array, and all you have to do is check that the array contains a value that you are interested in, then you can use the In_array () function to easily accomplish this function. The following example's program displays the string "Cannot find the value you're looking for in the array" ("Not Found in"), because the string you're looking for "albert" is not really $namesArray this array:

? $namesArray = Array ("Heart", "Love", "Boy", "Mary", "Paul", "Merry", "Jacky");
$lookingFor = "Albert";
if (In_array ($lookingFor, $namesArray)) {
echo "Found it! ";
} else {
echo "Can't find the value you're looking for in the array!" ";
}
? >

If you change the value of the variable $lookingfor to "mary" and then perform it again, the screen will show "Found!" "(" You ' ve found it! " Because the value of "mary" does exist in the $namesarray array. If you want to know the total number of elements contained in an array, you can use the Easy-to-use count () function:

? $namesArray = Array ("Heart", "Love", "Boy", "Mary", "Paul", "Merry", "Jacky");
$count = count ($namesArray);? >

The value of the variable $count will be 7.

2. Increase of array elements
You can easily add an element to the head or end of an array.

The following example first demonstrates how to use the Array_push () function to add elements to the end of an array:

? /* First we set up an array */
$fruitArray = Array ("Apple", "orange", "banana", "Peach", "pear");
/* Use the Array_push () function to add some elements to the end of the existing array.
Array_push ($fruitArray, "Grape", "pineapple", "tomato");
/* Now displays the keys (key) and value (value) of all elements of the array on the Web page.
while (the list ($key, $value) = each ($fruitArray)) {
echo "$key: $value <br>";
}
? >

The results appear as follows:

0:apple
1:orange
2:banana
3:peach
4:pear
5:grape
6:pineapple
7:tomato

Now let's demonstrate how to add some elements from the beginning of the array. The following program code is almost exactly the same as the previous one, except that the function used here is array_unshift () instead of Array_push ().

?
/* First we set up an array */
$fruitArray = Array ("Apple", "orange", "banana", "Peach", "pear");
/* Use the Array_unshift () function to add some elements to the beginning of the original array * *
Array_unshift ($fruitArray, "Grape", "pineapple", "tomato");
/* Now displays the keys (key) and value (value) of all elements of the array on the Web page.
while (the list ($key, $value) = each ($fruitArray)) {
echo "$key: $value <br>";
}
? >

The results appear as follows:

0:grape
1:pineapple
2:tomato
3:apple
4:orange
5:banana
6:peach
7:pear

3. Merging of arrays
The Array_merge () function merges two or more arrays into a new array, and when merging the elements of an array, the order of the merges is determined in the order in which the original array was called. If the original array itself has been reordered, then after the array merge completes, you must reorder the new array that is generated. The following example describes how this function is used:

? /* First set up first array * *
$fruitArray = Array ("Apple", "orange", "banana", "Peach", "pear");
/* Next set up a second array * *
$vegArray = Array ("carrot", "green beans", "asparagus", "artichoke", "corn");
/* Now use the Array_merge () function to merge two arrays into a new array.
$goodfoodArray = Array_merge ($fruitArray, $vegArray);
/* Finally, we will display the keys (key) and value (value) of all elements of the merged new array on the Web page.
while (the list ($key, $value) = each ($goodfoodArray)) {
echo "$key: $value <br>";
}
? >

The results appear as follows:

0:apple
1:orange
2:banana
3:peach
4:pear
5:carrot
6:green Beans
7:asparagus
8:artichoke
9:corn

4. Deletion of array elements
After introducing how to add elements and merging arrays, let me now describe how to remove some elements from an array. First, if you want to remove elements from the end of an array, we can use the Array_pop () function. There is also a function called Array_shift (), which is used to remove some elements from the beginning of the array. After we remove the element from the array, the array will no longer be able to find the element we deleted before, but the deleted element can still get its value through a variable.

Now we try to remove some elements from the end of the array using the Array_pop () function:

?
/* First we set up an array */
$fruitArray = Array ("Apple", "orange", "banana", "Peach", "pear");
/* Use the Array_pop () function to remove an element from the end of the array * *
$popped = Array_pop ($fruitArray);
/* Now we will display the keys (key) and value (value) of all elements in the deleted array on the Web page.
while (the list ($key, $value) = each ($fruitArray)) {
echo "$key: $value <br>";
}
Echo <br> Finally, the value of the element that has just been deleted is stored in the \ $popped variable, and its value is: $popped ";
? >

The results appear as follows:

0:apple
1:orange
2:banana
3:peach

Finally, the value of the element that was just deleted is stored in the $popped variable, and its value is: Pear.

Next, you'll demonstrate how to delete an element from the beginning of the array:

?
/* First we set up an array */
$fruitArray = Array ("Apple", "orange", "banana", "Peach", "pear");
/* Use the Array_shift () function to remove an element from the beginning of the array * *
$shifted = Array_shift ($fruitArray);
/* Now we will display the keys (key) and value (value) of all elements in the deleted array on the Web page.
while (the list ($key, $value) = each ($fruitArray)) {
echo "$key: $value <br>";
}
Echo <br> Finally, the value of the element that was deleted is stored in the \ $shifted variable, and its value is:
$shifted ";
? >

The results appear as follows:

0:orange
1:banana
2:peach
3:pear

5. Ordering of arrays
Finally, the value of the element that was deleted is stored in the $shifted variable, and its value is: Apple.

PHP4.0 provides a number of functions that can be used to reorder elements of an array, but here I'll just introduce the basics of sorting so that you can understand the process of array ordering:

? /* First we set up an array */
$fruitArray = Array ("Apple", "orange", "banana", "Peach", "pear");
/* Use the sort () function to reorder the elements of an array.
Sort ($fruitArray);
/* Reset ($fruitArray) so that we can correctly display it from beginning to the beginning/
/* Now we will display the keys (key) and value (value) of all elements in the deleted array on the Web page.
while (the list ($key, $value) = each ($fruitArray)) {
echo "$key: $value <br>";
}
? >

The results appear as follows:

0:apple
1:banana
2:peach
3:orange
4:pear


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.