An array is a data structure that stores one or more similar type of values in a single value.
For example, if you were to store 100 numbers, then instead of defining 100 variables, it is easy to define an array of 100 length. Each value in the array is accessed using an id, which is called array index.
These arrays can store numbers, strings and any object, but their index will be represented by numbers. By default the array index starts from 0.
<?php
$num = array("1", "2", "3", "4", "5");
foreach($num as $val){
echo $val;
}
?>
Associative arrays are similar to numeric arrays in terms of functionality, but they are different in terms of their index.
Associative arrays will have their index as string so that a strong association between key and values is established.
<?php
$salary = array(
"John" => 2000,
"James" => 1000,
"Zara" => 2500,
);
echo "John's salary is ".$salary['John'];
//echo for the others
?>
A multi-dimensional array is one in which each element in the main array can also be an array, and each element in the sub-array can be an array as well. And it goes on and on.
Values in the multi-dimensional array are accessed using multiple index.
<?php
$marks = array("John" => array("Physics" => 30, "Math" => 20, "English" => 10), "Musa" => array("Physics" => 10, "Math" => 10, "English" => 10));
echo "John's score in physics is ".$marks['John']['Physics'];
?>
See PHP documentation for arrays
Neumorphism design, alternatively termed as "soft UI" or "new skeuomorphism," represents a design trend that…
Debugging JavaScript code can sometimes be challenging, but with the right practices and tools, you…
Service Workers are one of the core features of modern web applications, offering powerful capabilities…
A database-driven website is a dynamic site that utilizes a database to store and manage…
Toasts are user interface elements commonly used in software applications, especially in mobile app development…
JavaScript has long been synonymous with frontend web development, powering interactive and dynamic user interfaces…