0
0
PHPprogramming~5 mins

Multidimensional arrays in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a multidimensional array in PHP?
A multidimensional array is an array that contains one or more arrays inside it. It is like a table or grid where each element can be accessed using multiple keys or indexes.
Click to reveal answer
beginner
How do you access the element 'PHP' in this array?<br>
$arr = [['HTML', 'CSS'], ['JavaScript', 'PHP']];
You access 'PHP' by using two indexes: $arr[1][1]. The first index selects the second array, and the second index selects the second element inside it.
Click to reveal answer
intermediate
How to add a new element 'Python' to the second array in a multidimensional array?
Use $arr[1][] = 'Python'; to add 'Python' at the end of the second array inside the multidimensional array.
Click to reveal answer
beginner
What will count($arr) return if $arr = [[1,2], [3,4], [5,6]];?
It will return 3 because count() counts the number of elements in the outer array, which has three inner arrays.
Click to reveal answer
intermediate
How can you loop through all elements in a multidimensional array?
Use nested foreach loops: the outer loop goes through each inner array, and the inner loop goes through each element inside those arrays.
Click to reveal answer
What does $arr[0][1] access in a multidimensional array?
AThe first element of the second inner array
BThe first element of the first inner array
CThe second element of the first inner array
DThe second element of the second inner array
How do you add a new inner array to a multidimensional array $arr?
A<code>$arr[] = ['new', 'array'];</code>
B<code>$arr = ['new', 'array'];</code>
C<code>$arr[0][] = 'new';</code>
D<code>array_push($arr[0], ['new', 'array']);</code>
What will count($arr, COUNT_RECURSIVE) do?
ACount all elements including those in inner arrays
BCount only the outer array elements
CCount only the inner array elements
DCause an error
Which loop structure is best to iterate over a multidimensional array?
AWhile loop only
BNested foreach loops
CSingle for loop
DDo-while loop only
What type of data structure is a multidimensional array similar to?
AQueue only
BSingle list
CStack only
DTable or grid
Explain what a multidimensional array is and how you access its elements in PHP.
Think of it like a table with rows and columns.
You got /2 concepts.
    Describe how to loop through all elements of a multidimensional array and why nested loops are needed.
    Imagine opening each box inside a bigger box.
    You got /2 concepts.