9 Array – II
Hiren Joshi
Multidimensional Array
A multidimensional array is also known as 2-D (two-dimensional) array. It can be thought as array of arrays. A two dimensional array is a tabular array. Following figure helps you to understand 2-D array structure.
To create the above array, the code is
$stud = array(array(1,’Rajan’),
array(2,’Haresh’),
array(3,’Pankaj’));
In the above figure, ID and Name is considered as attributes of array $stud.
To access the element of 2D array, two indexes values are required. The first index value refer to the outer array element and the second index value refer to the inner array element.
To access the first index value of second array element index value , you have to code
echo “<br> ID: “.$stud[1][0];
This will display ID: 2 on the browser.
This array is considered as nested array also. Here, the nested array inside an outer array is of same size. The size of nested array is same, then such type of array is known as rectangular array. In case the size of nested array is different, then this type of array is known as jagged array.
$jugstud = array(array(1,’Rajan’),
array(2,’Haresh’,’Mehta’),
array(3,’Pankaj’),
array(‘Mehul’));
echo “<br>”;
foreach($jugstud as $outkey=>$outval)
{
foreach($outval as $inkey=>$inval)
{
echo ‘<t> ‘. $inval;
//echo ‘<t> ‘. ‘outerkey : ‘. $outkey .’ inkey :’ .$inkey.’ inval: ‘. $inval; // echo ‘<t> ‘.$inkey .’ ‘. $inval; // this will print key as well }
echo “<br>”;
}
You can create more than 2D array also in the same way.
To understand the functions let’s write few programs.
range functions in array
range function is used to create an array. The general syntax for range( ) function is range($lo, $hi [, $step]).
The range function create an array with value start from $lo and fill the values to $hi by incement $step. If $step is omitted, then default $step 1.
$nums = range(1,10,1);
foreach($nums as $n)
{
echo “<t> $n “;
}
The above code create an array $nums having values 1,3,5,7,9.
Array_push and Array_pop:
These functions are called stack implementation in array. Stack structure is used LIFO (Last In First Out) approach.
$names = array(“Hiren”,”Vipul”,”Vimal”);
array_push($names,’Devdatt’); // $names is “Hiren”,”Vipul”,”Vimal”, “Devdatt”
echo “<br>”;
foreach($names as $name)
{
echo “<t> $name”;
}
echo “<br>”;
$last = array_pop($names); //$names is “Hiren”,”Vipul”,”Vimal”
foreach($names as $name)
{
echo “<t> $name”;
}
echo “<br> $last”; //$last = “Devdatt”
Array_unshift and Array_shift:
Array_push and array_shift functions are used to make queue implementation in array. Queue structure is used FIFO (First In First Out) approach.
//array_unshift
array_unshift($names,”Kalpesh”); //$names is Kalpesh Hiren Vipul Vimal
echo “<br>”;
foreach($names as $name)
{
echo “<t> $name”;
}
//array shift
$first = array_shift($names); // $names is Hiren Vipul Vimal
echo “<br>”;
foreach($names as $name)
{
echo “<t> $name”;
}
echo “<br> First : $first”; // First is Kalpesh
Array_count_values function demo
echo “<br>”;
$friends = array(“Hiren”,”Vimal”,”Vipul”,”Vimal”,”Devdatt”, “Hiren”,”Kalpesh”); echo “<br>”;
foreach($friends as $fr)
{
echo “<t> $fr”;
}
$occurence = array_count_values($friends);
echo “<br> Hiren occurs $occurence[Hiren] times”;
echo “<br> Vimal occurs $occurence[Vimal] times”;
echo “<br> Vipul occurs $occurence[Vipul] times”;
echo “<br> Devdatt occurs $occurence[Devdatt] times”;
echo “<br> Kalpesh occurs $occurence[Kalpesh] times”;
Then the output will be
Hiren Vimal Vipul Vimal Devdatt Hiren Kalpesh
Hiren occurs 2 times
Vimal occurs 2 times
Vipul occurs 1 times
Devdatt occurs 1 times
Kalpesh occurs 1 times
Sort function demo
$emp = array(‘Heena’,’Yatin’,’Chirag’,’Siddhi’,’Divya’);
echo “<br> Before sorting an array <br>”;
foreach ($emp as $e)
{
echo “<t> $e”;
}
sort($emp);
echo “<br> After sorting an array <br>”;
foreach ($emp as $e)
{
echo “<t> $e”;
}
Then the output will be
Before sorting an array
Heena Yatin Chirag Siddhi Divya
After sorting an array
Chirag Divya Heena Siddhi Yatin
rsort demo
rsort($emp);
echo “<br> Reverse Sorting <br>”;
foreach ($emp as $e)
{
echo “<t> $e”;
}
Output will be
Reverse Sorting
Yatin Siddhi Heena Divya Chirag
ksort demo
<?php
$milk = array(‘gold’=>24,’diamond’=>26,’taza’=>20,’shakti’=>22);
echo “<br> Before sorting an array <br>”;
foreach ($milk as $m=>$v)
{
echo “<br> $m price is Rs. $v”;
}
ksort($milk);
echo “<br><br> After ksort an array: “;
foreach ($milk as $m=>$v)
{
echo “<br> $m price is Rs. $v”;
}
?>
Output will be
Before sorting an array
gold price is Rs. 24
diamond price is Rs. 26
taza price is Rs. 20
shakti price is Rs. 22
After ksort an array:
diamond price is Rs. 26
gold price is Rs. 24
shakti price is Rs. 22
taza price is Rs. 20
Shuffle function example.
<?php
//Shuffle demo
echo “<br><br>”;
$values = array(‘2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’10’,’J’,’Q’,’K’,’A’); $types = array(‘C’,’D’,’H’,’S’);
echo ‘<br> The playing card are <br>’;
$cards = array();
foreach($types as $t)
{
foreach($values as $v)
{
$cards[] = $t.$v;
}
}
foreach($cards as $card)
{
echo “<t> $card”;
}
echo ‘<br> After shuffle the 5 cards are <br> ‘;
shuffle($cards);
$hand = array();
for($i =0; $i<5;$i++)
{
$hand[] = array_shift($cards);
}
echo implode(‘ ‘,$hand);
?>
When you execute the function, then the Output can be
The playing card are
C2 C3 C4 C5 C6 C7 C8 C9 C10 CJ CQ CK CA D2 D3 D4 D5 D6 D7 D8 D9 D10 DJ DQ DK DA H2 H3 H4 H5 H6
H7 H8 H9 H10 HJ HQ HK HA S2 S3 S4 S5 S6 S7 S8 S9 S10 SJ SQ SK SA
After shuffle the 5 cards are
S7 H4 D3 CJ HQ
you can view video on Array – II |
References:
1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,
2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional,Apress
3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K.Glass: Beginning PHP5, Apache, and MySQL Web Development, Wrox,
4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O’Reilly Media
5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP, Wrox
6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible
7. Joel Murach, Ray Harris: Murach’s PHP and MySQL, Shroff/Murach
8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML/Javascript/DHTML/PHP , BPB Publications
9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams
10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education
11. http://www.php.net
12. http://www.w3schools.com/
13. http://www.tutorialspoint.com/