You are on page 1of 26

2.

12 Arrays
• Arrays is a collection of similar type of elements
but in PHP you can have the elements of mixed
type together in single array.
• In each PHP, each element has two parts key and
value.
• The key represents the index at which the value
of the element can be stored.
• The keys are positive integers that are in
ascending order.
2.12.1 Array Creation
<html >
• In PHP there are two
<head>
types of arrays –
<title> PHP Indexed Arrays<
– Indexed array
/title >
– Associated array
</head>
• Indexed array : Indexed <body>
array are the arrays <?php
with numeric index. The $names = array("AAA", "BBB",
array values can be "CCC");
stored from index 0 . // Printing array structure
print _r($names);
?></body></html>
• Here values gets stored at
corresponding index as
follows -
• $mylist[O]= 10;
• $mylist[l] =20;
• $mylist[2] =30;
• $mylist[3] =40;
• $mylist[4] =50;
• We can directly assign some
value at specific index.
• $mylist[5] = 100;
• 2. Associated array : <html >
<head>
Associated arrays are
<title>PHP Associative Array</title>
the arrays with named </head>
keys. It is a kind of array <body>
with name and value <?php
pair. $city["AAA"]= "Pune";
$city["BBB"]= "Mumbai"; .
$city["CCC"]= "Chennai";
// Printing array structure
print_r($city);
?>
</body>
</html>
2.12.2 Accessing Array Elements
• Using an array subscript we can access the array element. The
value of subscript is enclosed with in the square brackets. For
example -
• $Citycode['Pune'] = 005;
• $Name[O]="Chitra";
• Multiple values can be set to a single scalar variableusing array.
For example -
• $people= array("Meena","Teena","Heena");
• list($operator,$accountant,$manager)=$people;
• By this assignment Meena becomes operator, Teena becomes
accountant and Heena becomes the manager.
• Ex. 2.12.1 Consider an associative array called
person_age with name and age of 10
persons. Write a PHP program to calculate
the average age of this associative array AU
<html > // Printing array structure
<head> $sum =array_sum($person
<title>PHP Associative _age);
Array</title> print("The sum of all the ages
</head> is $sum");
<body> $avg=$sum/10;
<?php print("<br/>The average is
$person _age =array('AAA' $avg");
=>10,'BBB' =>22,'CCC' =>
45,'DDD'=> 31,'EEE' =>33,'FFF'
?></body></html>
=>25,'GGG' =>56,'HHH' output: the sum of all age is
=>73,'Ill' = > 35,'JJJ' =>47); 377 The average is 37.7
2.12.3 Functions for Dealing with Arrays

<?php
$mylist=array(10,20,30,40,
50);
unset($mylist [1]);
for($i=0;$i < =4;$i + +)
{
print $mylist[$i];
print “ “ ;
}?>
• The functions array_keys and array_values are used to return
the array keys and the values at corresponding key. For
example consider the following PHP document
<?php
$mylist=array(10= > "AAA",20 = >"BBB",30= > "CCG',40= >"DDD",50=
>"EEE");
$Roll = array _keys ($mylist);
$Name = array_ values($mylist);
Print_r($Roll);
print "<br/>";
print_r($Name);
?>
• The existence of particular key can be checked by
using the array_key_exists function. This
functionreturns the Boolean value.
• The is_array returns the Boolean value. This
function takes a variable as a parameter. If the
function returns TRUE then that means the
parameter passed to this function is of array type
• The implode and explode functions are used to
break the word into strings or vice versa.
<?php $newSentence="I Like Programming
$mylist = In It";
array("Hello","PHP","You","Are"," $chunks =explode(" ",$newSentence);
Wonderfull!!!"); echo "The new sentence =
$mySentence = implode(" ", $mylist); $newSentence";
for($i = 0; $i < count($mylist); $i+ +) print "<br/>";
{ echo "The first chunk= $chunks[0]";
echo "$i = $mylist[$i] <br />"; print "<br/>";
} echo "The second chunk=$chunks[l]";
echo "The sentence is = print "<br/>";
$mySentence <br />"; echo "The third chunk=$chunks[2]";
print "<hr/>"; print "<br/>";
?>
Output
Sequential Access to Array Elements

• The array element reference start at the first


element and every array maintains an internal
pointer using which the next element can be
easily accessible. This helps to access the array
elements in sequential manner.
• The pointer current is used to point to the
current element in the array. Using the next
function thenext subsequent element can be
accessed.
<?php $myval= next ($mylist);
$mylist = print("The next value of the array
array("Hello","PHP","You","Are is <b>$myval</b>");
","Wonderful!!!"); print "<br/>";
$myval=current($mylist); $myval= next($mylist);
print("The current value of the print("The next value of the array
array is <b>$myval</b>"); is <b>$myval</b>");
print "<br/>": print "<br/>";
$myval=:next($mylist); $myval= next($mylist);
print("The next value of the array print("The next value of the array
is <b>$myval</b>"); is <b>$myval</b>");
print "<br/>"; ?>
<?php
$mylist =
array("Hello","PHP","You",
"Are","Wonderful!!!");
while($myval =each($mylist))
{
$val=$myval["value"];
print "<br/>";
}
?>
• For each function is used to <?php
iterate through all the $mylist = array(“Hello",
elements of the loop. "PHP","You","Are","Wonderful!
• The syntax of for each !!");
statement is as follows - foreach($mylist as $value)
foreach($array as $value) {
{ print("The current value of the
statements to be executed array is
} <b>$value</b>");
• The above code can be print "<br/>";
modified and written as }?>The output will be the same
follows - as above.
Sorting Arrays
• Sorting is a process in which the elements of arrays in
some specific order. There are two types ordering
which are followed in sorting - ascending order and
descending order.
• Basically PHP uses sort function for sorting the array
elements. There are some other functions that are
also available for sorting the arrays in desired manner.
• The sort function sorts the array based on the values.
After applying the sort function this function assigns
new keys to the values of the array.
• <?php
• $A = array("A" = > "Supriya", "B"= >
"Monika","C"= >"Kumar","D"=
>"Archana"):
• print "<b> Original Array: <b><br/>";.
• foreach($A as $key = > $value )
• print(" [$key] = >$value <br />");
• sort($A);
• print "<br/>";
• print "<b>Sorted Array:<b><br/>";
• foreach($A as $key= >$value)
• print("[$key] = >$value <br />");
• ?>
• The asort ( ) function sorts all array by the
values. But the original keys are kept.
• The ksort function sorts the array by keys but
each value's original key is retained.
• <?php • print "<,hr/>";
• $A = array("A"=> "Supriya", "B"=> • $B = array(30 => "CCC", 20 =>
"Monika","C"=> "BBB", 40 => "DDD",lO=>"AAA");
"Kumar","D"=>"Archana"); • print "<b>Original Array:<b><br/>";
• print "<b>Original • foreach($A as $key = > $value )
Array:<b><br/>"; • print(" [$key] = >$value <br I>");
• foreach($A as $key = > $value ) • ksort($A); .
• print(" [$key] = >$value <br />"); • print "<br/>";
• asort($A); • print "<b>Sorted Array by ksort
• print "<br/>"; function:<b><br/>";
• print "<b>Sorted Array by asort • foreach($A as $key= >$value)
function:<b><br/>"; • print("[$key]=>$value <br />");
• foreach($A as $key= >$value) • ?>
• print("[$key]= >$value <br />");
• Use an array to store student information
such as enrollment no, name, semester and
percentage. Output the data to a web page
using PHP:
• Sol. :<html > • for($i=0;$i <3;$i+ +)
• <head></head> • {
• <body> • echo "<tr>":
• <?php • for($j=0;$j<4;$j++ )
• $a=array(array(l0,"AAA","II",60),ar • {
ray(20,"BBB","III",80),array • echo "<td >";
(30,"CCC","IV",40)); • echo $a[$i][$j];
• echo "<table border =’1’>"; • echo "</td>";
• echo "<tr>"; • }
• echo"<td> ENo, </td><td> Name • echo "</tr>";
</td><td>Sem </td><td> • }
Marks</td>"; • echo "</table>";
• echo "</tr>"; • ?></body></html>

You might also like