What is the difference between a one-dimensional and a multi-dimensional array in PHP?
A one-dimensional array in PHP is a simple list of values stored under a single variable name, while a multi-dimensional array is an array of arrays, allowing for more complex data structures with multiple levels of nested arrays. To access elements in a one-dimensional array, you use a single index, while in a multi-dimensional array, you use multiple indexes to access elements at different levels of nesting.
// One-dimensional array
$colors = array("red", "green", "blue");
// Multi-dimensional array
$students = array(
array("name" => "John", "age" => 25),
array("name" => "Jane", "age" => 22),
array("name" => "Alice", "age" => 27)
);
// Accessing elements in a one-dimensional array
echo $colors[0]; // Output: red
// Accessing elements in a multi-dimensional array
echo $students[1]["name"]; // Output: Jane
Keywords
Related Questions
- What are the best practices for handling operands in PHP calculations?
- How can PHP developers ensure that emails are received by the intended recipients and not marked as spam or blocked by email filters?
- How can PHP developers efficiently extract specific values from multidimensional arrays or objects like in the provided example?