Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

PHP ARRAY

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.
An array is a data structure that stores one or more similar type of values in a single
value.
For example if you want to store 100 numbers then instead of defining 100 variables
its easy to define an array of 100 length.
An array is nothing but the data structure that allows the storage of multiple values under a
single name. In simple terms, it’s a variable that is capable of containing more than one values.
The values assigned to the array are homogeneous in nature, it means the data type of all the
values should be the same. If an array is supposed to be containing the integer value only then
it won’t be possible to store character or string values in that.
Eg:
$food1 = "Pizza";
$food2 = "Burger";
$food3 = "French Fry";
❑Advantage of PHP Array
• Less Code: we don’t need to define multiple variables.
• Easy to traverse: we can traverse all the elements of an array.
• Sorting: We can sort the elements of array.
• Less memory utilization:The increasing number of variables also
increases the memory utilization by the program as memory is required
to be assigned to every variable but in case of an array, memory has to
be assigned to the single array that could store multiple values and
same unnecessary memory consumption
Traverse an array means to access each element (item) stored in the array so that the
data can be checked or used as part of a process.
Types of Array in PHP
▪ Indexed or Numerical arrays - Arrays with a numeric index.
▪ Associative arrays - Arrays with named keys.
▪ Multidimensional arrays - Arrays containing one or more arrays.
Numerical or Indexed Arrays
A numerical array is a type of array which can store strings, numbers, and
objects and their index is represented by a number.
▪ First way to declare indexed Array
Syntax:
<?php
$variable_name[n] = value;
?>
Where,
“$variable_name…” is the name of the variable
“[n]” is the access index number of the element
“value” is the value assigned to the array element.
Eg:
<?php
$movie[0]="Shaolin Monk";
$movie[1]="Drunken Master";
$movie[2]="American Ninja"; Output:
$movie[3]="Once upon a time in China";
$movie[4]="Replacement Killers";
echo $movie[3];
$movie[3] = " Eastern Condors";
echo $movie[3];
?>
▪ Second Way: Using Array() Function
Syntax:
$array_name = array("value1", "value2", "value3",..)
Where,
• Array_name: is the name of the array that will consist of the values.
• Array(): is the function which helps to implement the mechanism of
the array in PHP.
• Values inside the double quotes are the values that are being
assigned to the array.
Eg:
<?php
$student_name=array("Amit", "Raj", "Dhiraj", "Shyam");
echo $student_name[0];
echo "<br>";
echo $student_name[1]; Output: The above program will print the
echo "<br>"; name of all the students and below is the
output.
echo $student_name[2];
Amit
echo "<br>"; Raj
Dhiraj
echo $student_name[3]; Shyam

?>
Eg:2
<html>
<head>
<title>Indexed Array</title>
</head>
<body>
<h1>Indexed Array Demo</h1>
<?php
$marks=array(60,78,87,67,80);
echo "Marks are : $marks[0],$marks[1],$marks[2] and
$marks[3]";
?>
</body> Output
</html>
Using Foreach
Syntax: foreach($array_name as $variable_represent_array_value){
}

Output
Using For ,while and do while loop

Output
Output
Output
Viewing Array Structure and Values
The var_dump keyword 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.
Output
f
<?php
$numbers = array('8', '20', '40', '58', '88', '200', '400', '500');
var_dump ($numbers [4]);
?>
Output:
The print_r() function is a built-in function in PHP and is used to print
or display information stored in a variable.

Output
Associative array
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.
Eg: <?php
$age= array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43")
;
echo "Peter is " . $age['Peter'] . " years old.";
?>
<?php
$age
= array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {


echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
Output
Multidimensional Arrays
The multidimensional array is an array in which each element can also
be an array and each element in the sub-array can be an array or
further contain array within itself and so on.
Eg:1 Output
Eg: 2 Output
Array functions
https://www.w3schools.com/php/php_ref_array.asp

You might also like