In This Lesson:
Some array functions we will cover for now
There are a lot of different things that you can do with arrays, but we will only begin to address some of them here to get started. Below are some of the functions we will discuss. If you are interested in exploring more array functions, visit the php.net array functions page.
- count : Retrieves the number of items in an array. Counts all data types.
- max : Retrieves the maximum value in an array. This will look at integers, floats, arrays, and strings, where it takes the following order, from highest to lowest:
- the highest positive integer or float
- strings (which have a numerical value lower than any positive integer or float but a value higher than zero). If a string is selected as a higher value than something else, it will take the highest alphabetical ordered string (i.e. “xray” instead of “animal”).
- Note: When max() is given multiple arrays, the longest array is returned.
- zero “0”
- negative integers or floats
- min : Retrieves the minimum value in an array. This will look at integers, floats, NULL, and strings, where it takes the following order, from lowest to highest:
- the lowest negative integer or float
- zero “0”
- strings (which have a numerical value lower than any positive integer or float but a value higher than zero). If a string is selected as the min value, it will take the lowest alphabetical ordered string (i.e. “animal” instead of “xray”).
- lowest positive integer or float
- sort : sorts all array items from lowest to highest and takes the data type ranking behavior listed above for the min() function. You must use a display method to see the sorted items on the screen. Merely “echoing” the sort function of an array will return the Boolean value of “1” to the screen to show that the sort is TRUE.
- rsort : Reverse sort.
- implode : This allows you to place a pattern between each array item so that you can echo the array to screen with separated values in a single string. For instance, placing a <br /> tag pattern in between each array item would help us list each array value on a new browser line.
- explode : This works in reverse order: it takes each separate piece of a single string that is separated by a certain pattern (like a forward slash character) and places each separate piece into an array as a value. For instance, if we are trying to separate out the values of a url address like “something.com/products/shoes/athletic/index.php”, you would end up with an array of the following keys => values:
[0] => something.com
[1] => products
[2] => shoes
[3] => athletic
[4] => index.php
The Examples
First, let’s set up some example arrays to work with, as well as a string to explode:
$numbers = array(1,8,3.5,4,-5,6.7);
$strings = array("dog","cat","bird","fish");
$mixed = array(0,"john",-6.5,"andy", array(0,2), NULL, 6);
$url = "something.com/products/shoes/athletic/index.php";
Using these examples, let’s experiment with our functions to see how they will behave.
count()
echo count($numbers); // will return to the browser: 6
echo count($strings); // will return to the browser: 4
max()
max($numbers); // will return to the browser: 6.7
max($strings); // will return to the browser: fish
max($mixed); // will return to the browser: 6
min()
min($numbers); // will return to the browser: -5
min($strings); // will return to the browser: bird
min($mixed); // will return to the browser:
hmm…why is the last value blank? That’s because the minimum value in $mixed
is NULL, which is like saying NOTHING, which is “less” than zero and negative numbers, and which displays as a big blank nothing in the browser. Also, the nested array in $mixed
is not a data type that the min or max functions consider in their selection processes, although they are distributed after the highest number..
sort()
sort($numbers); print_r($numbers); // will return to the browser:
Array ( [0] => -5 [1] => 1 [2] => 3.5 [3] => 4 [4] => 6.7 [5] => 8 )sort($strings); print_r($strings); // will return to the browser:
Array ( [0] => bird [1] => cat [2] => dog [3] => fish )sort($mixed);
echo "<pre>";
print_r($mixed);
echo "</pre>"; // will return to the browser:
Array (
[0] =>
[1] => -6.5
[2] => 0
[3] => andy
[4] => john
[5] => 6
[6] => Array ([0] => 0
[1] => 2
)
)
rsort()
rsort($mixed);
echo "<pre>";
print_r($mixed);
echo "</pre>"; // will return to the browser:
Array (
[0] => Array (
[0] => 0
[1] => 2
)[1] => 6
[2] => john
[3] => andy
[4] => 0
[5] => -6.5
[6] =>
)
**Notice that even though the reverse sort goes from highest to lowest, it did not sort the order of the nested array. Thus, this function does not have a cascading affect through multidimensional arrays.
implode()
echo $imploded_string = implode("<br />",$strings); // will return to the browser:
bird
cat
dog
fish
echo $imploded_string = implode("/",$strings); // will return to the browser:
bird/cat/dog/fish
explode()
print_r(explode("/",$url)); // will return to the browser:
Array ( [0] => something.com [1] => products [2] => shoes [3] => athletic [4] => index.php )
**Remember, the $url
variable was originally a simple string data type, but the explode()
function turned it into an array.
More Advanced Uses of Arrays
Loops
We haven’t covered loops yet, but the most effective way of accessing an array is through the use of a loop, which literally loops through, or “steps” through, each item of an array to perform tasks with each item.
If you are interested in checking this out, visit the Loops lecture.