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 potential pitfalls of using do-while loops in PHP, as seen in the provided code snippet?
- What are common pitfalls when inserting data into a MySQL database using PHP?
- In the context of uploading multiple files with different expiration dates, how can PHP handle the logic to ensure each file's deadline is respected?