How can you create a multidimensional array in PHP?

Prepare for the Zend Certified PHP Engineer Exam with our comprehensive test, featuring flashcards and multiple choice questions. Each question comes with detailed hints and explanations. Ensure you're ready for your exam!

To create a multidimensional array in PHP, you nest arrays within arrays. This structure allows you to have an array that contains other arrays as its elements, which can be used to represent complex data structures. For example, if you want to create an array of students where each student entry includes their name and scores in subjects, you define it like this:

$students = [
['name' => 'John', 'scores' => [85, 90, 78]],
['name' => 'Jane', 'scores' => [88, 76, 92]],

];


In this example, the main array `$students` contains multiple arrays. Each of these inner arrays holds the information for an individual student, demonstrating how nesting can effectively create a multidimensional structure.

The other options do not effectively lead to creating a multidimensional array. Using a single array refers to a one-dimensional array, which cannot hold arrays themselves. Declaring multiple variables would simply create multiple independent values, and while combining associative arrays can be a method of data organization, it does not inherently create the multidimensional aspect without explicitly nesting.
Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy