What is an array in PHP?
In PHP, an array is a data structure that stores multiple values in a single variable. Arrays can hold values of different types (such as integers, strings, and objects) and are particularly useful for managing collections of related data.
Here are the key types of arrays in PHP:
-
Indexed Arrays: These use numeric indexes to access elements. The index starts from 0. For example:
$fruits = array("Apple", "Banana", "Cherry"); echo $fruits[1]; // Outputs "Banana"
-
Associative Arrays: These use named keys to access elements instead of numeric indexes. For example:
$person = array("first_name" => "John", "last_name" => "Doe", "age" => 30); echo $person["first_name"]; // Outputs "John"
-
Multidimensional Arrays: These are arrays containing one or more arrays. They can be used to represent more complex data structures. For example:
$contacts = array( "John" => array("phone" => "123-456-7890", "email" => "john@example.com"), "Jane" => array("phone" => "987-654-3210", "email" => "jane@example.com") ); echo $contacts["John"]["email"]; // Outputs "john@example.com"
Arrays in PHP are flexible and can be used to build various kinds of data structures, making them a fundamental part of PHP programming.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.