In: PHP
24 Feb 2010An array is a data structure that stores one or more values in a single value. A variable can store only one data in memory. When we need to store more than one values we use Array to store data. Array understand data by its index. It’s a collection of elements having same name but different index.
For Example:
$var="PHP is good.";// Store only PHP is good.
$var=array("PHP is Good.", "I Like PHP");//Store both elements in $var variable. And when we need to retrieve data we can call its index like $var[0].
Syntax:
$array("element1", "element2", " n element");
A simple Example of Array
<?php
$arr=arry(10,20,30,40,50);
print_r($arr);//Prints Structure of Array.
?>
Array having string as a key is called an associative array. which means that it is a type that maps values to keys
Example
<?php
$arr=array('a'=>'apple', 'b'=>'banana', 'c'=>'cat');
echo ("Array Elements are <br>");
foreach($arr as $key=>$val)
{
echo ("key:$key value: $val");
echo ("<br>");
}
?>
Sorting Numeric Array
<?php
$arr=array(25, 15, 5, 10, 20, 40, 30);
sort($arr);//Sorts an array in ascending order
echo ("Array elements in ascending order<>");
foreach($arr as $item){
echo ($item."<br>");
}
rsort($arr);//Sorts array in descending order
echo ("Array elements in descending order<>");
foreach($arr as $item){
echo ($item."<br>");
}
?>
If a key is not specified for a value, the maximum of theĀ integerĀ indices is taken and the new key will be increment by 1. If a key that already has an assigned value is specified, that value will be overwritten.
Example:
<?php
// This array is the same as ...
$a=array(5 => 43, 32, 56, "b" => 12);
foreach($a as $key=>$val){
echo ("Key is $key, Value is $val<br>");
}
// ...this array
array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>
Sorting Associative array:
When sorting associative key asort() function removes the key of associative array and set index values. So when we need to sort associative array with key values we need to use asort() function. To sort according to key we can use ksort() for ascending order and krsort() for desecending order.
Example:
<?php
$country=array("np"=>"Nepal", "in"=>"India", "us"=>"America", "ch"=>"China", "jp"=>"Japan");
asort($country);//Sort associative array with key values.
foreach($country as $key=>$val){
echo("$key => $val<br>");
}
arsort($country);//sorts associative array in descending order
echo ("<br/> Descending Order<br>");
foreach($country as $key=>$val){
echo("$key=>$val<br>");
}
?>
Shorting by key for ksort() and krsort:
<?php
$country=array("np"=>"Nepal", "in"=>"India", "us"=>"America", "ch"=>"China", "jp"=>"Japan");
ksort($country);//Sort associative array with key values.
foreach($country as $key=>$val){
echo("$key => $val<br>");
}
krsort($country);//sorts associative array in descending order
echo ("<br/> Descending Order<br>");
foreach($country as $key=>$val){
echo("$key=>$val<br>");
}
?>
The arrray having another arrays as its elements (OR arrays with in array) is called multidimensional array. That means If an array element value is an other array then this is a multidimensional array.
For Example:
$arr=array(array(10,20,30), array('a', 'b', 'c'), array(10.5, 15.2, 20.44));
Display and Sorting Multidimensional Array:
<?php
$arr=array(array(10,20,30), array('a', 'b', 'c'), array(10.5, 15.2, 20.44));
foreach($arr as $num)
{
foreach($num as $val)
{
echo ($val. "<br>");
}
echo ("<br><br>");
}
?>
Sorting Multidimensional Array:
<?php
$arr=array(array(50,20,30), array('a', 'f', 'c'), array(17.5, 15.2, 20.44));
foreach($arr as $num)
{
sort($num);// Sort Array.
foreach($num as $val)
{
echo ($val. "<br>");
}
echo ("<br><br>");
}
?>

5 Responses to PHP Array Functions
levitra
March 2nd, 2010 at 2:40 pm
Thank you! You often write very interesting articles. You improved my mood.
prescription
March 3rd, 2010 at 8:14 am
Interesting and informative. But will you write about this one more?
FexNaireafrip
March 7th, 2010 at 6:07 pm
Thanks for writing, I very much liked
buy generic drugs
March 8th, 2010 at 7:00 pm
In truth, immediately i didn’t understand the essence. But after re-reading all at once became clear.
Nitesh
March 11th, 2010 at 6:26 am
I liked it. So much useful material. I read with great interest.