Array functions in PHP
You probably know that an array is a collection of variables taking up the same space. In PHP an array has the same meaning like in other programming languages. Here is an example of representing an array: $fruits[ ];
Merging arrays
You can merge two arrays easily using array_merge function. Here I have the two arrays:
<?php$fruits = array(1 => "apples",2 => "oranges",3 => "bananas"); $vegetables = array(1 => "potatoes",2 => "tomatoes");?>
You can combine these two arrays into one using:
<?php $health_food = array_merge ($fruits, $vegetable); ?>
The newly formed array called “health_food” will contain all 5 entries from the two arrays. The index for health_food will appear from 0 to 4.
$health_food[0] = "apples"; $health_food[1] = "oranges"; $health_food[2] = "bananas"; $health_food[3] = "potatoes"; $health_food[4] = "tomatoes";
Counting Arrays
You can count the number of elements in an array using a command called count. If we want to count the elements for the fruits[] array we can have:
<?php $total_elements = count($fruits); cho "There are $total_elements elements in the array.";?>
This will produce the result :
There are 3 elements in the array.
Accessing Arrays
You can access an individual element in an array by calling a specific index value or try each command.
<?php
$count_total = count($fruits);
for ($i=0; $i<$count_total; $i++){
$line = each ($fruits);
echo "$line[key] $line[value] <br />";
}?>
What I’ve done here? First I have calculated the total number of elements. For loop starts a counter set to zero and continue as the counter value ($i) is less than the number of entries in the array ($count_total). As we know the counter is increased after each loop is completed. The $line variable is a temporary array variable. It will hold two values per each loop: one value is the index and the second value is the value for the specified key. I used the echo command to print the results.
1 apples 2 oranges 3 bananas
Using Arrays
in_array(search word, array) is a function very useful while working with arrays, especially for filtering certain items. Using this function you can find easily a value. See the example:
<?php$fruits = array("apple","orange","strawberry");
if(in_array("apple", $fruits)){
echo "Apple is in the array";}
else{echo "Apple is not in the array";}
?>
You can use this for blocking access for some IPs. Here is an example:
<?php$blocked_IPs = array("86.126.22.86", "192.168.1.12", "192.168.1.13");
$ip = $REMOTE_ADDR;if(in_array($ip, $blocked_IPs)){
echo "You have been banned from this site";
}else{//normal page goes here}
?>
Sorting Arrays
Sorting values means organize them on certain criteria. PHP can be used to sort an array by it’s keys or values. The result can keep the corresponding key/value with the original value/key or replace them. PHP uses the next function:
- sort: this command is used to sort the values with no restriction for the key. The values are put in alphabetical order but the keys remain in the same order - sort($fruits);
- rsort: is similar with sort but here values are sorted is a reverse order-rsort($fruits);
- asort: is used for sorting values but keeping the corresponding keys -asort($fruits);
- arsort: similar with asort but with a reverse sorting - arsort($fruits);
- ksort: sort the keys and keep the values -ksort($fruits);
- krsort: sort the keys and keep the values in reverse order -krsort($fruits);
- shuffle: is used to randomly reorganize the values of an array, keys remains the same. shuffe($fruits);
Example:
If we apply asort($fruits) command the array will looks:
1 apples 3 bananas 2 oranges
Push and Pop
If you heard about stack and queue concepts you also heard about ways for adding or removing elements. Using array_push( ) function you will add a new element to the end of the array. Using array_pop( ) command you can delete the last found array entry. The index numbers will remain the same, but the value will become empty.
<?php $fruits_vegetables = array(1 => "tomatoes",0 => "oranges",3 => "bananas",2 => "potatoes"); array_push($fruits_vegetables, "apples");?> Result:oranges tomatoes potatoes bananas apples
array_pop($pantry);oranges tomatoes bananas

RSS/XML