4 Array
4 Array
4 Array
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