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.
The following is the referenced content: $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:
The following is the referenced content: $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 ():
The following is the referenced content: /* 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:
The following is the referenced content:
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 ().
The following is the referenced content: /* 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:
The following is the referenced content:
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.
The following is the referenced content: /* 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:
The following is the referenced content:
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.
- Total 2 Pages:
- Previous page
- 1
- 2
- Next page
http://www.bkjia.com/PHPjc/364192.html www.bkjia.com true http://www.bkjia.com/PHPjc/364192.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 ...