Array PHP
Array PHP
An array is a special variable that we use to store or hold more than one value in a
single variable without having to create more variables to store those values. To create
an array in PHP, we use the array function array( ).
Types of array
1. Numeric/Indexed Arrays
2. Associative Arrays
3. Multidimensional Arrays
Example :
<?php
// Numeric/ index arrays
$cars = array('Mecedes Benz', 'Hilux', 'Highlander', 'Hummer', 'Limozien');
var_dump($cars);
?>
Output:
array(5) { [0]=> string(12) "Mecedes Benz" [1]=> string(5) "Hilux" [2]=> string(10)
"Highlander" [3]=> string(6) "Hummer" [4]=> string(8) "Limozien" }
The var_dump($cars) keyword above will show us the total number of elements we
have in the array, the index number of each array, and also the length of each element
in the array.
We can also chose to use the echo( ) keyword, but var_dump( ) gives a more detailed
explanation of the results.
We can also choose to display only one element/item of an array in the web browser
by doing this:
<?php
$numbers = array('8', '20', '40', '58', '88', '200', '400', '500');
var_dump ($numbers [4]);
?>
Output:
string(2) "88"
Associative Arrays
An associative array is a type of array where the key has its own value. In an
associative array, we make use of key and value.
Keys are descriptive captions of the array element used to access the value of the
array. And value is the value assigned to the array element.
There are situations where you shouldn't use the numeric/indexed array, such as:
When we want to store the age of different students along with their names.
When we want to record the salaries of your employees.
When we want to store the score of a student in different subjects
and so on.
Suppose we want to assign ages to a group of high school students with their names.
We can use the Associative array method to get it done.
Example:
<?php
$student_age = array (
'Scott_Mcall' => 17,
'Stalenski' => 18,
'Lydia' => 16,
'Allision' => 17,
);
echo $student_age ['Scott_Mcall']."<br>";
echo $student_age ['Stalenski']."<br>";
echo $student_age ['Lydia']."<br>";
echo $student_age ['Allision']."<br>";
?>
Output:
17
18
16
17
The keys of the array are scott_Mcall, Stalenski, Lydia, Allision, and we used them to
assign the age to each student. The values of the array are 17, 18, 16, and 17.
Multidimensional Arrays
A multidimensional array is an array of arrays. This means that every element in the
array holds a sub-array within it. In general, multidimensional arrays allows us to
store multiple arrays in a single variable.
Suppose we want to store the Names, Registration Numbers, and Emails of some of
the staff working in a particular company. We can use multidimensional arrays to
archive this.
Example:
<?php
$Staffs = [
[
'Name' => 'Derek Emmanuel',
'Reg_No' => 'FE/30304',
'Email' => 'derekemmanuel@gmail.com'
],
[
'Name' => 'Rubecca Michealson',
'Reg_No' => 'FE/20003',
'Email' => 'rmichealsongmail.com'
],
[
'Name' => 'Frank Castle',
'Reg_No' => 'FE/10002',
'Email' => 'fcastle86@gmail.com'
]];
echo $Staffs [2] ['Email'];
echo $staffs [0] ['Name'];
echo "<br><br>";
//Using for and foreach in nested form
$keys = array_keys($staffs);
for($i=0;$i<count($staffs);$i++)
{
echo $keys[$i]."<br>";
foreach($staffs[$keys[$i]] as $key => $value)
{
echo $key." => ".$value."<br>";
}
echo "<br>";
}
?>
Output:
fcastle86@gmail.com
Derek Emmanuel
0
Name => Derek Emmanuel
Reg_No => FE/30304
Email => derekemmanuel@gmail.com
1
Name => Rubecca Michealson
Reg_No => FE/20003
Email => rmichealsongmail.com
2
Name => Frank Castle
Reg_No => FE/10002
Email => fcastle86@gmail.com
For-each
The foreach loop is used for iterating over an array. The code block is executed for
every element in the array and the value of that element is available for use in the
code block.
The syntax is:
On each iteration, a $value from $array is available for usage in the code block.
To access the keys as well as values in an array, the syntax can be modified to:
Example : 1
<?php
// Declare an array
$arr = array("green", "blue", "pink", "white");
?>
Output:
green blue pink white
Example : 2
<?php
$employee = array(
"name" => "Robert",
"email" => "robert123@gmail.com",
"age" => 20,
"gender" => "male"
);
//Loop through employee array
foreach($employee as $element)
{
echo $element."<br>";
}
echo "<br><br>";
foreach($employee as $key => $element)
{
echo $key.":".$element."<br>";
}
echo "<br><br>";
?>
Output:
Robert
robert123@gmail.com
20
male
name:Robert
email:robert123@gmail.com
age:20
gender:male
Syntax:
var_dump(variable);
The array_keys() function returns an array containing all the keys or a subset of the
keys of the array provided. array_keys() can operate on both indexed and associative
arrays.
Syntax
array_keys($array, $filter_value, $strict)
Example
This example extracts all the keys from the array. The array $arr contains numeric and
string indexes.
<?php
$arr = array("hello" => "world", "hi", "greeting" => "welcome", 5 => "hello");
print_r(array_keys($arr));
?>
Output:
Array
(
[0] => hello
[1] => 0
[2] => greeting
[3] => 5
)