Basic codes for PHPWith PHP 4.0 comes more than 30 new array-related functions. Some of the more common functions let you determine if something is in a given array or not, count the number of elements in an array, add or delete array elements, and sort array elements.
If you have a large array and all you need to do is find out if a given value exists, you can use in_array() to return true or false. The following will print "Not found in this array" because you're looking for "Albert" in the $namesArray, which does not exist.
Code:
<? $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 receive the "You've found it!" message, since "Mary" is part of the $namesArray.
If you want to count the number of elements in an array, you can use the handy count() function:
Code:
<? $namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
$count = count($namesArray); ?>
The value of $count is 7.
You can add elements to any array, either at the end or the beginning of an existing array. You can also use array_merge() to create a new array consisting of the elements of two or more arrays. When merging, each array is tacked on in the order that it is requested. If your arrays have an internal sort order already in place, you'll have to re-sort your new, merged array.
Let's start with adding elements at the end of an existing array, using
array_push():
Code:
<? /* create the original array */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* add to the original array */
array_push($fruitArray, "grape", "pineapple", "tomato");
/* list each element, with its key */
while (list($key,$value) = each($fruitArray)) {
echo "$key : $value<br>";
}
?>
This will display:
0 : apple
1 : orange
2 : banana
3 : kiwi
4 : pear
5 : grape
6 : pineapple
7 : tomato
The code is remarkably similar if you need to add some elements to the beginning of an array,. The only difference is the function name: array_unshift() instead of array_push().
Code:
<?
/* create the original array */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* add to the original array */
array_unshift($fruitArray, "grape", "pineapple", "tomato");
/* list each element, with its key */
while (list($key,$value) = each($fruitArray)) {
echo "$key : $value<br>";
}
?>
This will display:
0 : grape
1 : pineapple
2 : tomato
3 : apple
4 : orange
5 : banana
6 : kiwi
7 : pear
The array_merge() function smashes two or more arrays together.
Code:
<? /* create the first array */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* create the second array */
$vegArray = array("carrot", "green beans", "asparagus", "artichoke", "corn");
/* merge the arrays into one */
$goodfoodArray = array_merge($fruitArray, $vegArray);
/* list each element, with its key */
while (list($key,$value) = each($goodfoodArray)) {
echo "$key : $value<br>";
}
?>
This will display:
0 : apple
1 : orange
2 : banana
3 : kiwi
4 : pear
5 : carrot
6 : green beans
7 : asparagus
8 : artichoke
9 : corn
Now that you've added and merged arrays, let's run through the functions for deleting elements from arrays. You can delete one element from the end of an array, using array_pop(). If you use array_shift(), you're deleting an element from the beginning of an array. While you are in fact deleting the element from the array, this element is still available to you as a variable, when you pop it or shift it from the existing array.
Try using array_pop() to delete a value from the end of an array:
Code:
<?
/* create an array */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* pop something off the end */
$popped = array_pop($fruitArray);
/* list the contents of the new array, as well as the value you popped off */
while (list($key,$value) = each($fruitArray)) {
echo "$key : $value<br>";
}
echo "<br>and finally, in $popped: $popped";
?>
This will display:
0 : apple
1 : orange
2 : banana
3 : kiwi
and finally, in $popped: pear
Next, delete an element from the end of an array:
Code:
<?
/* create an array */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* shift something from the beginning */
$shifted = array_shift($fruitArray);
/* list the contents of the new array, as well as the value you shifted off */
while (list($key,$value) = each($fruitArray)) {
echo "$key : $value<br>";
}
echo "<br>and finally, in $shifted: $shifted";
?>
This will display:
0 : orange
1 : banana
2 : kiwi
3 : pear
and finally, in $shifted: apple
There are several functions that assist you in sorting array elements, but I'll show you just the basic sort so that you understand the process:
Code:
<? /* create the original array */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* sort the array */
sort($fruitArray);
/* reset it so you can display it properly from beginning to end */
/* list each element, with its key */
while (list($key,$value) = each($fruitArray)) {
echo "$key : $value<br>";
}
?>