What is PHP Arrays |
What is an Array?
An array is a variable but it is a special variable. Because it can hold more than one value at a time. Using an array you can store multiple different different value in a single variable. You can use an array to store multiple values. It is so easy to create an array and also you can easily access the array values from an array with the index number. Generally, an array index number start with 0.
$university = "IUBAT";
$university = "BUET";
$university = "DU";
An array can store multiple values in one single variable:
In the below, there is an array named university. The array named university contains three values named IUBAT, BUET and DU. The index position of value "IUBAT" is 0. The index position of value "BUET" is 1 and the index position of value "DU" is 2.
Now, if you want to access the array values from this array named university. Then you can easily access values with the index number. Here in the below, I am going to show how can access array values from an array.
<!DOCTYPE html>
<html>
<body>
<?php
$university = array("IUBAT", "BUET", "DU");
echo "University Name - " . $university[0] . ", " . $university[1] . " and " . $university[2] . ".";
?>
</body>
</html>
The Result of the above program:
University Name - IUBAT
University Name - BUET
University Name - DU
<!DOCTYPE html>
<html>
<body>
<?php
$university = array("IUBAT", "BUET", "DU");
echo "University Name - " . $university[0] . ", " . $university[1] . " and " . $university[2] . ".";
?>
</body>
</html>
The Result of the above program:
University Name - IUBAT
University Name - BUET
University Name - DU
The Result of the above program:
University Name - IUBAT
University Name - BUET
University Name - DU
Create an Array in PHP:
The array() function is used to create a array:
array();
Types of array:
There are three types of arrays:- Indexed arrays - Arrays with a numeric index
- Associative arrays - Arrays with named keys
- Multidimensional arrays - This type Arrays containing one or more arrays
PHP Indexed Arrays:
There are two ways to create indexed arrays:The index can be assigned automatically and always starts at 0, like this:
$university = array("BUET", "IUBAT", "DU");
$university[0] = "BUET";
$university[1] = "IUBAT";
$university[2] = "DU";
<!DOCTYPE html>
<html>
<body>
<?php
$university = array("IUBAT", "BUET", "DU");
echo "University Name - " . $university[0] . ", " . $university[1] . " and " . $university[2] . ".";
?>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<?php
$university = array("BUET", "IUBAT", "DU");
$arrlength = count($university);
for($a = 0; $a < $arrlength; $a++) {
echo $university[$a];
echo "<br>";
echo "<br>";
}
?>
</body>
</html>
The Result of the above program:
University Name - IUBAT
University Name - BUET
University Name - DU
PHP Associative Arrays:
Associative arrays are arrays that use named keys that you assign to them as key.Two ways to create an associative array:
$age = array("bangla"=>"5", "tutorial"=>"5", "point"=>"5");
$age['bangla'] = "5";
$age['tutorial'] = "5";
$age['point'] = "5";
What is the use of Associative Array?
Suppose you are going to store employees information of a school such as employees salaries. Then you need to use associative array to store the employees salaries.
When you are going to store an employees salaries at first you need at least two field like as employee name and salary amount. You have to use associative array. Because associative array contains key with value.
That's way i think you need to use associative array in this case.
The named keys can then be used in script:
<!DOCTYPE html>
<html>
<body>
<?php
$salary = array("John"=>"5000", "Abhram"=>"2000", "Sinha"=>"3000");
$salary = array("John"=>"5000", "Abhram"=>"2000", "Sinha"=>"3000");
echo "The salary of John is " . $salary['John'];
?>
</body>
</html>
The result of above program -
The salary of John is 5000
The result of above program -
The salary of John is 5000
Loop Through an Associative Array:
Using loop and print all the values of an associative array, you could use a foreach loop, like this:
<!DOCTYPE html>
<html>
<body>
<?php
$salary = array("John"=>"5000", "Abhram"=>"2000", "Sinha"=>"3000");
foreach($salary as $key => $value) {
echo "Name is=" . $key . ", Salary is=" . $value; echo "<br>";
}
?>
</body>
</html>
The result of above program -
Name is = John, Salary is= 5000
Name is = Abhram, Salary is= 2000
Name is = Sinha, Salary is= 3000
The result of above program -
Name is = John, Salary is= 5000
Name is = Abhram, Salary is= 2000
Name is = Sinha, Salary is= 3000
PHP - Multidimensional Arrays:
The Multidimensional array is an array containing one or more arrays.PHP supports multidimensional arrays that are two, three, four, five, or more levels deep.
However, arrays more than three levels are hard to manage for most people.
What is the use of Multidimensional Array?
Suppose you are going to store the marks of the students. You have to use multidimensional array to store the marks of the students. Because There are many students in a school and each student has different subject with different marks. Multidimensional array contains array of array. That's why we can use multidimensional array to store students marks.PHP - Two-dimensional Arrays:
A two-dimensional array is an array of arrays.
A two-dimensional array, like this:
$university = array (
array("IUBAT",10,5),
array("BUET",12,15),
array("DU",52,20),
array("RUET",16,10)
);
<!DOCTYPE html>
<html>
<body>
<?php
$university = array (
array("IUBAT",10,5),
array("BUET",12,15),
array("DU",52,20),
array("RUET",16,10)
);
echo $university[0][0].": Male Student: ".$university[0][1].", female Student: ".$university[0][2].".<br>";
echo $university[1][0].": Male Student: ".$university[1][1].", female Student: ".$university[1][2].".<br>";
echo $university[2][0].": Male Student: ".$university[2][1].", female Student: ".$university[2][2].".<br>";
echo $university[3][0].": Male Student: ".$university[3][1].", female Student: ".$university[3][2].".<br>";
?>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<?php
$university = array (
array("IUBAT",10,5),
array("BUET",12,15),
array("DU",52,20),
array("RUET",16,10)
);
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$university[$row][$col]."</li>";
}
echo "</ul>";
}
?>
</body>
</html>
Get The Length of an Array:
The count() function is used to return the length of an array. It means the number of elements of an array :
<!DOCTYPE html>
<html>
<body>
<?php
$university = array("BUET", "IUBAT", "DU");
echo count($university);
?>
</body>
</html>
Result:
3
EmoticonEmoticon