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.
<?php
$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:
<?php
$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():
<?php
/* 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
";
}
?>
This will display:array_unshift() instead of array_push().
<?php
/* 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
";
}
?>
This will display:array_merge() function smashes two or more arrays together.
"; } ?>This will display:
array_pop() to delete a value from the end of an array:
"; } echo "This will display:
and finally, in $popped: $popped"; ?>
<?php
/* 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
";
}
echo "
and finally, in $shifted: $shifted";
?>
This will display:sort() so that you understand the process:
<?php
/* 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
";
}
?>
This will display: