Arrays in PHP
Arrays in PHP: Introduction- What is an Array?
What is an array?
· An array is a single variable that holds multiple data. In PHP, it enhances the scope and flexibility of variables.
· When we create any variable, we can store just a single value, but an array provides the ability to store multiple values under a single variable name.
· In PHP, we can store different types of data in a single element, like int, float, and character-like data in a list form.
· PHP supports single-dimensional, two-dimensional, and multidimensional arrays.
· There were three types of arrays
1. Index array: In indexed arrays, array elements have a numerical index that starts from 0. Indexed array initialized as well as executed with numerical index.
2. Associative array: Associative arrays allow us to create a named key for each element of an array. With this key, we can access or execute the array precisely.
3. Multidimensional array: Multidimensional arrays allow us to create more than one array within an array. We can create it in an index or an associative format.
· Array elements are stored and accessed by their index id, which starts from zero. Associative array stores and retrieves values by its key value(name). It makes it easier to handle and recognize which kind of data is stored.
· We can create an array using the array function and square brackets [ ].
Example: Display array using HTML and PHP
<?php
$a=array("a1"=>array("rno"=>1,"name"=>"ram","city"=>"latur"),"a2"=>array("rno"=>2,"name"=>"ram","city"=>"latur"));
echo "<table border=1>";
echo "<hd><td>Rno</td><td>name</td><td>city</td></th>";
foreach($a as $x1=>$y1)
{ echo "<tr>";
foreach($y1 as $x=>$y)
{
echo"<td>$y</td>";
}
echo"</tr>";}
echo"</table>";
?>
OUTPUT
2.4 Types of Arrays: Indexed Vs. Associative arrays, Multidimensional arrays
Indexed arrays:
· An Indexed array is a single variable that holds multiple data elements.
· In indexed arrays, array elements have a numerical index that starts from 0.
· An indexed array is initialized and executed with a numerical index. This numerical index is written with a pair of check braces [].
We can declare with a pair of square brackets [ ].
Syntax :
$array name = [];
$array name[0]= value1;
$array name[1]= value2;
$array name[2]= value3;
Example:
$a=[];
$a[0]= 1;
$a[1]= 2;
$a[2]= 3;
We can initialize an array in various ways, such as,
Syntax 1 with []:
$array name = [exp1,exp2…….exp n];
Example:
$a=[2,34,56,5];
Or
$a=[“Sunday”, ” Monday”, ” Tuesday”];
Syntax 2 with index number:
$arrayname[0]=value;
$arrayname[1]=value;
......
$arrayname[n]=value;
Example:
$a[0]=”Sunday”;
$a[1]=”Monday”;
$a[n]=”Tuesday”;
$ array name = array(exp1,exp2…….exp n);
Example:
$a=array(1,2,3);
Or
$a=array(“Sunday”, ” Monday”, ” Tuesday”);
How to execute indexed arrays in PHP?
Syntax:
Echo $arrayname [index value];
Example:
Echo $a[0];
Example:
<?php
$a=[];//array(); we can also use array function here.
$a[0]=1;
$a[1]=7;
$a[2]=13;
$a[3]=14;
$a[4]=15;
echo $a[0],"\n";
echo $a[1],"\n";
echo $a[2],"\n";
echo $a[3],"\n";
echo $a[4],"\n";
?>
OUTPUT
Execute an array with a for loop:
<?php
$a=[];
echo "INPUT\n";
for($a1=0;$a1<3;$a1++)
{
$a[$a1]=readline();
}
echo "\nOUTPUT\n";
for($a1=0;$a1<3;$a1++)
{
echo $a[$a1],"\n";
}
?>
Execute an array with a foreach:
<?php
$a=[“Sunday”, ” Monday”, ” Tuesday”];
foreach($a as $i)
{
echo $i,"\n";
}
?>
OUTPUT
Sunday
Monday
Tuesday
Associative arrays:
· Associative arrays allow us to create a named key for each element of an array.
· With this key name, we can access or execute the array elements more precisely.
· Associative key name followed by the “=>” operator, then value.
· Rather than an index number, we can use a key name to retrieve array elements. We can also use a for loop and a Foreach loop to execute an array.
Declare or initialize values
Syntax with []:
$ array-name[“key-name”]=value;
WE can create an associative array individually, one by one, giving a specific name.
Example:
$a=[];
$ a[“n1”]=1;
$ a[“n2”]=2;
Access array:
$a=[];
$ a[“n1”]=readline();
$ a[“n2”]=readline();
Execute array:
Echo $a[“n1”];
Echo $a[“n2”];
Syntax with array():
$array-name= array(“key1”=>value1, “key2”=>value2……..”key n”=>value n);
Example:
$a = array(“a”=>1,”b”=>2,”c”=>3);
Display an array:
Echo $a[“a”];
It will display the first element of the array.
Display array with foreach loop
<?php
$a=[“a1”=>“one”,”a2”=> ”Two”,”a3”=> ”Three”];
Foreach($a as $x=>$y)
{
Echo “$x=$y”
}
?>
OUTPUT
A1=one a2=Two a3=Three
Example 1:
<?php
$a["rno"]=readline("Enter roll number\t");
$a["name"]=readline("Enter name\t");
$a["city"]=readline("Enter city\t");
echo "roll no-\t".$a["rno"],"\n";
echo "Name-\t" ,$a["name"],"\n";
echo "City-\t",$a["city"],"\n";
?>
OUTPUT
Enter roll number 1
Enter name ram
Enter city latur
roll no- 1
Name- ram
City- latur
Comments
Post a Comment