PHP Arrays
Use this lesson when you want to understand the key concepts behind PHP Arrays.
An array stores multiple values in one single variable. Arrays are essential when working with lists of data, such as a list of users, products, or settings.
Indexed arrays use numeric indexes (starting at 0) to store and access values.
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Associative arrays are arrays that use named keys that you assign to them. These are very similar to dictionaries or hash maps in other languages.
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
A multidimensional array is an array containing one or more arrays. PHP supports deep multidimensional arrays, which are frequently used to store complex datasets from databases.
(Note: You can get the length of an array using the count() function).