Follow us on Facebook
Faucibus toroot menuts
PHP Array
Array is a group of elements which has common name. Array value can be accessed using it’s index.
Array exists in almost all programming languages. Array provides very powerful tools for manipulating data.
PHP provides a lot of array functions which makes the things easier.
In this tutorial, first we will see types of functions with examples and then we will also have a look at PHP in-built array functions.
Array Types
Types of arrays:
● Indexed array
● Associative Array
● Multidimensional Array
Indexed Array
● Indexed Array
In this kind of array key is hidden and start with sequential numeric index, such as 0,1,2 etc.
Example:
Output:
So in actual the above array is like this
$arr = array(); $arr[0] = ‘1’ $arr[1] = ‘2’ $arr[2] = ‘3’ $arr[3] = ‘4’
So, if you want to show value 3 then you have to check its index. And its index is, 2. Because array index always starts with 0.
Also in indexed array, we normally don't define its key.
Example:
Result
Associative Array
● Associative Array
The array in which user defines its value as well as key. And array key and value can be anything.
Example:
Output :
Multi-Dimensional Array
● Multi-Dimensional Array
In multi-dimensional array value of one array is array itself.Example:
Structure
Based on above structure if you want to show Canada then you will have to write like this
echo $world_map[2][1];
here is complete code :
Output