0
0
PHPprogramming~10 mins

Indexed array creation in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Indexed array creation
Start
Create empty array
Add element at index 0
Add element at index 1
Add element at index 2
Array ready with elements
End
We start with an empty array and add elements one by one at increasing numeric indexes, creating an indexed array.
Execution Sample
PHP
<?php
$array = ["apple", "banana", "cherry"];
print_r($array);
?>
This code creates an indexed array with three fruits and prints it.
Execution Table
StepActionArray StateIndex AssignedOutput
1Create empty array[]N/ANo output
2Add 'apple'["apple"]0No output
3Add 'banana'["apple", "banana"]1No output
4Add 'cherry'["apple", "banana", "cherry"]2No output
5Print array["apple", "banana", "cherry"]N/AArray ( [0] => apple [1] => banana [2] => cherry )
💡 All elements added, array printed, execution ends.
Variable Tracker
VariableStartAfter 1After 2After 3Final
$array[]["apple"]["apple", "banana"]["apple", "banana", "cherry"]["apple", "banana", "cherry"]
Key Moments - 3 Insights
Why does the first element get index 0 automatically?
In PHP, indexed arrays start numbering from 0 by default, so the first added element is at index 0 as shown in step 2 of the execution table.
What if I add elements without specifying indexes?
PHP automatically assigns the next available numeric index, increasing by 1 each time, as seen in steps 2 to 4 where indexes 0, 1, and 2 are assigned.
Why does print_r show indexes even if I didn't write them?
print_r shows the internal array structure including indexes, which helps us see the numeric keys PHP assigned automatically (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the array state after step 3?
A["apple", "banana", "cherry"]
B[]
C["apple", "banana"]
D["banana", "cherry"]
💡 Hint
Check the 'Array State' column at step 3 in the execution table.
At which step does the array get the element 'cherry'?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look for the action adding 'cherry' in the execution table.
If we added another element 'date' after 'cherry', what index would it get?
A3
B2
C0
D1
💡 Hint
Indexes start at 0 and increase by 1 for each new element, see variable_tracker for index progression.
Concept Snapshot
Indexed array creation in PHP:
$array = ["value1", "value2", "value3"];
Elements get numeric indexes starting at 0 automatically.
Use print_r($array) to see keys and values.
Adding elements without keys appends them with next index.
Full Transcript
This visual trace shows how PHP creates an indexed array by starting with an empty array and adding elements one by one. Each element gets a numeric index starting at 0 automatically. The execution table tracks each step: creating the array, adding 'apple' at index 0, 'banana' at index 1, and 'cherry' at index 2. Finally, print_r outputs the array showing the indexes and values. The variable tracker shows how the array grows after each addition. Common confusions include why indexes start at 0 and how PHP assigns indexes automatically. The quiz tests understanding of array state at different steps and index assignment rules.