An array is a data structure or a collection of data that contains a group of elements under same name.
Typically these elements are all of the same data types or different data types.
for example:
If a complex has thirty flats each flat having same features then the whole complex is considered to be an Array.
Each of thirty flat is an element of that array and as each flat shares the same type of features – those are considered as same data types.
So, in a programming language like PHP, the array is a collection of different type of value in a single variable.
You also can say a string is one kind of array because string consists of multiple characters.
Note: In programming languages like ‘C’, only one type of value (integer or string or float etc.) can be stored in an array and must be defined at the beginning of the program, but in PHP we can store different types of value (string, integer, Boolean, null, object, float) in a single array.
Define an array
An array is a data structure that stores one or more different type of values in a single variable.
Syntax of an array
1 2 3 4 | <?php $fruits = array(‘apple’,’mango’,’banana’,’lemon’); // syntax type I $fruits = [‘apple’,’mango’,’banana’,’lemon’]; // syntax type II** ?> |
Note: Most of developers use syntax type II for robust programming support.
How to print array in PHP
How to print an array in PHP?
Answer is very simple declare and initialize array use print_r() function to print array.
array elements are stored in continuous memory location
1 2 3 4 5 | <?php $fruits = [‘apple’,’mango’,’banana’,’lemon’]; print_r($fruits); //it is a pre-define function of PHP(we discuss about FUNCTION later) which use to display all elements of array on the browser. var_dump($fruits); //it is another pre-define function of PHP which also use to display all elements of array on the browser ?> |
Note: this two functions (print_r() and var_dump()) are also debugging function in PHP (about debugging function will be discussed later)
Output:
1 2 | array(0=>apple 1=>mango 2=>banana 3=>lemon) //output of print_r($fruits) array(4){0=>string(5)apple 1=> string(5)mango 2=> string(6)banana 3=> string(5)lemon } //output of var_dump($fruits) |
now 0 1 2 3 are index of the value means 0 1 2 3 in this position apple mango banana lemon are stored respectively.
Index means which is used to identify the particular one value/element in the array.
Explanation:
We can find a house by its address but when we try to find a room in an apartment there must be a number or floor division which separates every room/house from each other rooms/houses.
In similar way multiple value stores in single array separates by a unique index number.
It is also called an ‘ARRAY IDENTIFIRE’. Index, always starts from 0 and automatically incremented, but you also set index manually.
Example:
You can return particular one element of any array by using index number of this array.
1 2 3 4 5 | <?php $fruits = [‘apple’,’mango’,’banana’,’lemon’]; /* we want to print mango */ echo $fruits[1]; ?> |
1 | mango |
In another way you also define array in PHP:
1 2 3 4 5 6 7 | <?php $fruit[] = ‘Apple’; $fruit[] = ‘Mango’; $fruit[] = ‘Banana’; $fruit[] = ‘Lemon’; print_r($fruit); ?> |
Display an array
1 | array(0=>apple 1=>mango 2=>banana 3=>lemon) |
This is the example of manual index value. We can also use in this manner.
1 | Array(2=>Apple 5=>Banana 10=>Coconut 25=>Mango 30=>Strawberry) |
How to know the length or size of an unknown array
1 2 3 4 | <?php $fruits = [‘apple’,’mango’,’banana’,’lemon’]; print count($fruits); //count() function is a pre-define PHP function which return the length or size of any array. In PHP there is no size limitation or bounding to create an array. ?> |
Output
1 | 4 |
Printing array using loop in PHP
1 2 3 4 5 6 | <?php $fruits = [‘apple’,’mango’,’banana’,’lemon’,’lychee’,’cherry’,’orange’,’strawberry’,’watermelon’]; for($i=0;$i<count($fruits);$i++){ echo “$i : $fruits[$i] <br-->“; } ?> |
Result
1 2 3 4 5 6 7 8 9 10 | 0 : apple 1 : mango 2 : banana 3 : lemon 4 : lychee 5 : cherry 6 : apple 7 : orange 8 : strawberry 9 : watermelon |
Using FOREACH loop
1 2 3 4 5 6 | <?php $fruits = [‘apple’,’mango’,’banana’,’lemon’,’lychee’,’cherry’,’orange’,’strawberry’,’watermelon’]; foreach($fruits as $key=-->$value){ echo “$key : $value <br>“; } ?> |
Result
1 2 3 4 5 6 7 8 9 10 | 0 : apple 1 : mango 2 : banana 3 : lemon 4 : lychee 5 : cherry 6 : apple 7 : orange 8 : strawberry 9 : watermelon |
How to store values in array in php using for loop
1 2 3 4 5 6 7 8 9 | <?php $a = [‘apple’,’mango’,’banana’,’lemon’]; print_r($a); $c = array(); // for loop for ($i = 0; $i < count($a); $i++) { $c[$i] = $a[$i]; } ?> |
How to store values in array in php using forEach loop
1 2 3 4 5 6 7 8 9 | <?php $a = ['apple', 'mango', 'banana', 'lemon']; print_r($a); $c = array(); //forEach loop foreach ($a as $b) { $c[] = $b; } ?> |
How to store values in array in php using while loop
1 2 3 4 5 6 7 8 9 10 11 | <?php $a = ['apple', 'mango', 'banana', 'lemon']; print_r($a); $c = array(); //forEach loop $x = 0; while ($x < count($a)) { $c[$x] = $a[$x]; $x++; } ?> |
Output of all above is same
Output
1 | Array ( [0] => apple [1] => mango [2] => banana [3] => lemon ) Array ( [0] => apple [1] => mango [2] => banana [3] => lemon ) |