Array-In-PHP
What is an Array?
An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
$cars1 = "Volvo";
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
The solution is to create an array!
An array can hold many values under a single name, and you can access the values by referring to an index number.
Create an Array in PHP :
In PHP, the array() function is used to create an array:
array();
In PHP, there are three types of arrays:
Indexed arrays - Arrays with a numeric index.
Associative arrays - Arrays with named keys.
Multidimensional arrays - Arrays containing one or more arrays.
(1)PHP Indexed Arrays :
There are two ways to create indexed arrays:
The index can be assigned automatically (index always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
</body>
</html>
(2)PHP Associative Arrays:
Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array:
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
<!DOCTYPE html>
<html>
<body>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
</body>
</html>
(3)Multidimensional Associative Array:
<?php
$marks=array(
"mohammad"=>array
(
"physics"=> 35,
"maths"=> 30,
"chemistry"=>39
),
"zara"=>array
(
"physics"=> 35,
"maths"=> 40,
"chemistry"=> 21
),
"quara"=>array
(
"physics"=> 45,
"maths"=> 48,
"chemistry"=> 45
)
);
echo "value of mohammad array".'<br>';
echo $marks["mohammad"]["physics"].'<br>';
echo $marks["mohammad"]["maths"].'<br>';
echo $marks["mohammad"]["chemistry"].'<br>';
echo" value of zara array".'<br>';
echo $marks ["zara"]["physics"].'<br>';
echo $marks["zara"]["maths"].'<br>';
echo $marks["zara"]["chemistry"].'<br>';
echo"value of quara array".'<br>';
echo $marks["quara"]["physics"].'<br>';
echo $marks["quara"]["maths"].'<br>';
echo $marks["quara"]["chemistry"].'<br>';
?>
$marks=array(
"mohammad"=>array
(
"physics"=> 35,
"maths"=> 30,
"chemistry"=>39
),
"zara"=>array
(
"physics"=> 35,
"maths"=> 40,
"chemistry"=> 21
),
"quara"=>array
(
"physics"=> 45,
"maths"=> 48,
"chemistry"=> 45
)
);
echo "value of mohammad array".'<br>';
echo $marks["mohammad"]["physics"].'<br>';
echo $marks["mohammad"]["maths"].'<br>';
echo $marks["mohammad"]["chemistry"].'<br>';
echo" value of zara array".'<br>';
echo $marks ["zara"]["physics"].'<br>';
echo $marks["zara"]["maths"].'<br>';
echo $marks["zara"]["chemistry"].'<br>';
echo"value of quara array".'<br>';
echo $marks["quara"]["physics"].'<br>';
echo $marks["quara"]["maths"].'<br>';
echo $marks["quara"]["chemistry"].'<br>';
?>
OUTPUT :
value of mohammad array
35
30
39
value of zara array
35
40
21
value of quara array
45
48
45
35
30
39
value of zara array
35
40
21
value of quara array
45
48
45
(4)Multidimensional Index Array :
<?php
$abc=array
(
array(1,2,3),
array(4,5,6),
array(6,7,8)
);
echo"value of position 0,0=".$abc[0][0].'<br>';
echo "value of position 0,1=".$abc[0][1].'<br>';
echo "value of position 0,2=".$abc[0][2].'<br>';
echo "value of position 1,0=".$abc[1][0].'<br>';
echo "value of position 1,1=".$abc[1][1].'<br>';
echo "value of position 1,2=".$abc[1][2].'<br>';
echo "value of position 2,0=".$abc[2][0].'<br>';
echo "value of position 2,1=".$abc[2][1].'<br>';
echo "value of position 2,2=".$abc[2][2];
?>
OUTPUT :
value of position 0,0=1
value of position 0,1=2
value of position 0,2=3
value of position 1,0=4
value of position 1,1=5
value of position 1,2=6
value of position 2,0=6
value of position 2,1=7
value of position 2,2=8
==============================================================================================================================================
ARRAY IMPORTANT FUNCTIONS:
$abc=array
(
array(1,2,3),
array(4,5,6),
array(6,7,8)
);
echo"value of position 0,0=".$abc[0][0].'<br>';
echo "value of position 0,1=".$abc[0][1].'<br>';
echo "value of position 0,2=".$abc[0][2].'<br>';
echo "value of position 1,0=".$abc[1][0].'<br>';
echo "value of position 1,1=".$abc[1][1].'<br>';
echo "value of position 1,2=".$abc[1][2].'<br>';
echo "value of position 2,0=".$abc[2][0].'<br>';
echo "value of position 2,1=".$abc[2][1].'<br>';
echo "value of position 2,2=".$abc[2][2];
?>
OUTPUT :
value of position 0,0=1
value of position 0,1=2
value of position 0,2=3
value of position 1,0=4
value of position 1,1=5
value of position 1,2=6
value of position 2,0=6
value of position 2,1=7
value of position 2,2=8
==============================================================================================================================================
ARRAY IMPORTANT FUNCTIONS:
Comments
Post a Comment