Last Updated On By Khizer Ali
In this tutorial we are going to discuss how to initialize an Array in PHP. Array in PHP is a data structure that allows you to store different values in a single variable It is also known as a collection of data, which provides a way to group multiple variables into a single variable.
For Example, if you want to store names of different kinds of apples, then instead of defining separate variables, it is easy to define an array of the required length. In this article, you will learn how to initialize an array in PHP.
Table of Contents
There are three types of an array in PHP. Once you create an array, its item can be added, remove, altered, and much more.
The list of items can be of any data types. It means not all the elements in an array need to be of the same data type.
There are three types of arrays in PHP:
Note: You can also find the length of PHP Array.
An array having a digital index is known as a Numeric array or indexed array. It can store values of all data types, but the index should be numeric. By default, the array index starts from 0.
Here is a simple program of using a numeric array.
<?php
$apples = array("Fuji", "McIntosh", "Red Delicious", "Gala", "Jonagold");
foreach( $apples as $apple ) {
echo "$apple is a type of apple <br/> <br/>";
}
?>
In this example, we store different kinds of apples in a single variable $apples as an array with the help of array function. It will initialize an array with values.
To output those, we iterate through each item of the array and print that on the screen.
In PHP version 5.4 or higher, you can use the following syntax to declare and initialize an array.
<?php
$apples = ["Fuji", "McTntosh"];
//This is how you can add values after initializing it.
$apples[] = "Red Delicious";
$apples[] = "Gala";
$apples[] = "Jonagold";
foreach( $apples as $apple ) {
echo "$apple is a type of apple <br/> <br/>";
}
?>
This will provide the same result which the previous example produces.
Associative arrays are very similar to numeric array in terms of functionality, but in this, you can assign names to items of the lists, which are more user-friendly way to do that. You can even assign numbers as an index to an associative array. In this case, the arguments are in the form of key => value pair.
Suppose we are creating an array to store the currently available version of different programming languages.
<?php
$CurrentVersions = [
"php" => "7.4.5",
"python" => "3.7",
"java" => "14",
"C#" => "8.0",
];
?>
On the other hand, we can use the following syntax as well to define the associative array.
<?php
$CurrentVersions["php"] = "7.4.5";
$CurrentVersions["python"] = "3.7";
$CurrentVersions["java"] = "14";
$CurrentVersions["C#"] = "8.0";
?>
To access values, you can either use the index or use foreach to loop through associative array.
<?php
$CurrentVersions = [
"php" => "7.4.5",
"python" => "3.7",
"java" => "14",
"C#" => "8.0",
];
echo $CurrentVersions["php"];
foreach ($CurrentVersions as $lang => $version) {
echo "<br/> <br/> latest Version of $lang is $version";
}
?>
A multi-dimensional array in PHP in nothing more than an array, in which each member is itself an array. Each element in the sub-array can also be an array, and so on. This is how the multidimensional array works.
The following is the example of multidimensional associative array or we could say a two dimensional array because we need two indices to select an element.
<?php
$books = [
"THIS EXPLAINS EVERYTHING" => [
"author" => "John Brokman",
"Release date" => "January 22,2013",
"Price" => "$11.99"
],
"YOU ARE STARDUST" => [
"author" => "Elin kelsey",
"Release date" => "September 12,2012",
"Price" => "$13.29"
],
"THINKING IN NUMBERS" => [
"author" => "Daniel Tammet",
"Release date" => "2012",
"Price" => "$10.29"
]
];
echo "You are Stardust is written by " . $books["YOU ARE STARDUST"]["author"];
echo "<br/> Thinking in number was released on " .$books["THINKING IN NUMBERS"]["Release date"];
?>
In conclusion, Arrays in PHP are one of the most powerful data structures for storing a large number of values. In this article, you have covered most of the things, like declaring and initializing different kinds of arrays with code snippets and retrieving values through them. That is all you need to start coding with arrays.