- 5 years ago
- Zaid Bin Khalid
- 17,983 Views
-
6
In this tutorial, you’ll figure out how to store the number of values into an array with the help of PHP. The array index must be unique same index with different values just return the last index value.
What are PHP Arrays?
Arrays are the data structure where you can store data. Consider the below example you have three different colors that you want to store in a single variable. You can not do this task without the help of an array.
<?php
$color1 = "Red";
$color2 = "Green";
$color3 = "Blue";
?>
The array allows you to store the same or different types of data in a single variable. So, you can say the array is a special kind of variable where you can store more than one value at a time.
<?php
$colors = array("Red", "Green", "Blue");
?>
Let’s suppose you have to store all the student details. So, in this way you create an array where you can store all student data into an array. It is a very hard, exhausting, and impractical notion to store every student’s name in a different variable.
Create an array in PHP.
In PHP to create an array is very simple you can use array() function to create an array in PHP.
Types of Arrays in PHP.
There are three kinds of arrays that you can make. These are listed below.
- Indexed arrays – Arrays with a numeric index
- Associative arrays – Arrays with named keys
- Multidimensional arrays – Arrays containing one or more arrays
Filed Arrays.
The filled or indexed array is an array where all key values are numeric and always start from zero. You can define this array as below.
<?php
//Define an indexed array
$colors = array("Red", "Green", "Blue");
$colors = array(0=>"Red", 1=>"Green", 2=>"Blue");
?>
When you try to get the output of both array you will get the same result as shown in the below example.
Array
(
[0] => Red
[1] => Green
[2] => Blue
)
Associative Arrays.
In an Associative Arrays, the keys are well defined with respect to the data that index holds as shown in the below example. There may be a number of person with different or the same age.
<?php
$ages = array("Zaid"=>26, "Ali"=>30, "John"=>28, "Cris"=>28, "Clark"=>28);
?>
Multidimensional Arrays
In a multidimensional array, there are one or more arrays between the array. So, you can say the sub-array of the first array. The below example is a three-dimensional array example.
<?php
// Define a multidimensional array
$cars = array(
array("name"=>"Volvo","Modal"=>"GH-234","Grade"=>5),
array("name"=>"BMW","Modal"=>"PAJD-231","Grade"=>4),
array("name"=>"Saab","Modal"=>"OWED-7554","Grade"=>3.5,'parts'=>
array('light','engine','wheel')
),
array("name"=>"Land Rover","Modal"=>"LDKS-01454",4.5)
);
?>
When you try to print the above array you will get the below result.
Array
(
[0] => Array
(
[name] => Volvo
[Modal] => GH-234
[Grade] => 5
)
[1] => Array
(
[name] => BMW
[Modal] => PAJD-231
[Grade] => 4
)
[2] => Array
(
[name] => Saab
[Modal] => OWED-7554
[Grade] => 3.5
[parts] => Array
(
[0] => light
[1] => engine
[2] => wheel
)
)
[3] => Array
(
[name] => Land Rover
[Modal] => LDKS-01454
[0] => 4.5
)
)
How to get the value of array in PHP?
To get the value of array in PHP you just need to use the array variable and call its index as shown in the below code.
<?php
echo $cars[0]['name'];
//Volvo name will return.
echo $cars[2]['parts'][0];
//light part will return.
?>
- 5 years ago
- Zaid Bin Khalid
- 17,983 Views
-
6