what is an array
An array is a variable that can be used to hold multiple values at a time.There are two ways to create arrays:
by using array function
$names=array("om","lab as sistant",89);
by using [] brackets
$names=["om","lab assistan t",89];
$var_name=array(value_1,value_2,value_n);
or
$var_name=[value_1,value_2,value_n];
example:
$names=array("om","lab assistant" ,89);
or
$names=["om","lab assistant" ,89];
<?php
<?php
In PHP, there are three types of arrays:
- Indexed arrays - Arrays with a numeric index
$var_name=array(value_1,value_2,value_n);
or
$var_name=[value_1,value_2,value_n];
example:
$names=array("om","lab assistant" ,89);
echo $names[0], " <br>";
echo $names[2], " <br>";
echo $names[2], " <br>";
or
$names=["om","lab assistant" ,89];
- Multidimensional arrays - Arrays containing one or more array
two-dimensional arrays:
syntax:
$var_name = array (
array(value_1,value_2,value_n),
array(value_1,value_2,value_n),
array(value_1,value_2,value_n)
);
$emps = array (
array(101,103,108),
array("om","max","alax"),
array(1200 0,78000,67000)
);
Q1. create a two-dimensional array and print the value of row 1 and column 2
syntax:
$var_name = array (
array(value_1,value_2,value_n),
array(value_1,value_2,value_n),
array(value_1,value_2,value_n)
);
$emps = array (
array(101,103,108),
array("om","max","alax"),
array(1200 0,78000,67000)
);
Q1. create a two-dimensional array and print the value of row 1 and column 2
<?php
$emps = array (
array(101,103,108),
array("om","max","alax"),
array(1200 0,78000,67000)
);
array(101,103,108),
array("om","max","alax"),
array(1200 0,78000,67000)
);
echo $emps[0][1];
?>
three-dimensional array:
$var_name =array(
array (
array(value_1,value_2,value_n),
array(value_1,value_2,value_n),
array(value_1,value_2,value_n)
),
array (
array(value_1,value_2,value_n),
array(value_1,value_2,value_n),
array(value_1,value_2,value_n)
)
);
Q1. create a three dimensional array and print the value of table 1, row 2, and col 3
Q1. create a two-dimensional array and print the all values of array.
<?php
$emps = array (
array(101,103,108),
array("om","max","alax"),
array(12000,78000,67000)
);
for($r=0;$r<count($emps);$r++)
{
for($c=0;$c<count($emps[$r]);$c++)
{
echo$emps[$r][$c]," ";
}
echo"<br>";
}
?>
three-dimensional array:
$var_name =array(
array (
array(value_1,value_2,value_n),
array(value_1,value_2,value_n),
array(value_1,value_2,value_n)
),
array (
array(value_1,value_2,value_n),
array(value_1,value_2,value_n),
array(value_1,value_2,value_n)
)
);
Q1. create a three dimensional array and print the value of table 1, row 2, and col 3
<?php
$emps = array (
array(
array(101,103,108),
array("om","max","alax"),
array(1200,7000,6000)
),
array(
array(102,104,105),
array("om","max","alax"),
array(12000,78000,67000)
),
array(
array(106,109,110),
array("om","max","alax"),
array(1000,8000,7000)
),
);
echo $emps[0][1][2];
$emps = array (
array(
array(101,103,108),
array("om","max","alax"),
array(1200,7000,6000)
),
array(
array(102,104,105),
array("om","max","alax"),
array(12000,78000,67000)
),
array(
array(106,109,110),
array("om","max","alax"),
array(1000,8000,7000)
),
);
echo $emps[0][1][2];
?>
$var_name = array("key_1"=>"value_1", "key_2"=>"value_2", "key_n"=>"value_n");
Q1. create an associative array and show the data any key.
Q1. create a three-dimensional array and print the all values of array.
<?php
$emps=array(
array(
array(101,103,108),
array("om","max","alax"),
array(1200,7000,6000)
),
array(
array(102,104,105),
array("om","max","alax"),
array(12000,78000,67000)
),
array(
array(106,109,110),
array("om","max","alax"),
array(1000,8000,7000)
),
);
for($t=0;$t<count($emps);$t++)
{
for($r=0;$r<count($emps[$t]);$r++)
{
for($c=0;$c<count($emps[$t][$r]);$c++)
{
echo$emps[$t][$r][$c]," ";
}
echo"<br>";
}
echo"<br>";
}
?>
- Associative arrays - Arrays that can be used to hold multiple values in the form of keys and values.
$var_name = array("key_1"=>"value_1", "key_2"=>"value_2", "key_n"=>"value_n");
Q1. create an associative array and show the data any key.
<?php
$emp = array("name"=>"sagar", "age"=>10, "gender"=>"male");
$emp = array("name"=>"sagar", "age"=>10, "gender"=>"male");
echo $emp["name"];
?>
Q2. create an associative array and print the array.
?>
Q2. create an associative array and print the array.
<?php
$emp = array
( "name" => "sam",
"email" => "sam@gmail.com",
"age" => 22,
"gender" => "male"
);
foreach($emp as $key => $value)
{
echo $key . ": " . $value . "<br>";
}
?>
Q2. create an associative array that stores the names and ages of employess and then print only names of employess.
<?php
$emp = array(
"names"=>array("sam","max","alax"),
"age"=>array(43,34,34)
);
foreach($emp["names"] as $value)
{
echo $value . "<br>";
}
?>
0 Comments