0
0
PHPprogramming~5 mins

Indexed array creation in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an indexed array in PHP?
An indexed array in PHP is a list of values where each value is automatically assigned a numeric index starting from 0.
Click to reveal answer
beginner
How do you create an indexed array with three fruits: apple, banana, and cherry?
You can create it like this: <br> $fruits = array('apple', 'banana', 'cherry'); <br> or using the short syntax: <br> $fruits = ['apple', 'banana', 'cherry'];
Click to reveal answer
beginner
What index does the first element of an indexed array have in PHP?
The first element of an indexed array always has the index 0.
Click to reveal answer
beginner
How can you add a new element to the end of an existing indexed array?
You can add a new element by using the empty square brackets: <br> $fruits[] = 'orange'; <br> This adds 'orange' at the next available numeric index.
Click to reveal answer
beginner
What will be the output of print_r($fruits); if $fruits = ['apple', 'banana']; $fruits[] = 'cherry';?
The output will be:<br>
Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
)
Click to reveal answer
Which of the following creates an indexed array with values 10, 20, 30 in PHP?
A$numbers = <10, 20, 30>;
B$numbers = {10, 20, 30};
C$numbers = [10, 20, 30];
D$numbers = (10, 20, 30);
What is the index of the element 'banana' in $fruits = ['apple', 'banana', 'cherry'];?
A1
B0
C2
D3
How do you add 'grape' to the end of the array $fruits = ['apple', 'banana'];?
A$fruits['grape'] = true;
B$fruits[] = 'grape';
Carray_push($fruits, 'grape');
DBoth C and D
What will count($fruits); return if $fruits = ['apple', 'banana', 'cherry'];?
A2
B3
C0
DError
Which syntax is NOT valid for creating an indexed array in PHP?
A$arr = {1, 2, 3};
B$arr = [1, 2, 3];
C$arr = array(1, 2, 3);
D$arr = [];
Explain how to create an indexed array in PHP and how the indexes are assigned.
Think about how you list items in order without naming the keys.
You got /3 concepts.
    Describe two ways to add a new element to an existing indexed array in PHP.
    One way uses brackets, the other is a built-in function.
    You got /2 concepts.