Creating Indexed Arrays
Part of the Fundamentals section of Coddy's PHP journey — lesson 35 of 71.
Now that you understand what arrays are, let's look at how to create them. PHP gives you two ways to create an indexed array.
The most common approach uses square brackets:
<?php
$colors = ["Red", "Green", "Blue"];
?>You can also use the array() function, which does exactly the same thing:
<?php
$colors = array("Red", "Green", "Blue");
?>Both methods create identical arrays. The square bracket syntax is shorter and more modern, so you'll see it used more often in PHP code today.
Arrays can hold any type of data—strings, numbers, booleans, or even a mix:
<?php
$numbers = [10, 20, 30, 40];
$mixed = ["Hello", 42, true];
?>You can also create an empty array and add items to it later:
<?php
$tasks = [];
?>Remember, PHP automatically assigns index numbers starting from 0. So in $colors, "Red" is at index 0, "Green" at index 1, and "Blue" at index 2.
Challenge
EasyRead three values from input: a string fruit1, a string fruit2, and a string fruit3.
Create an indexed array called $fruits containing these three fruits in the order they were received.
Then, read two integers from input: num1 and num2.
Create an indexed array called $numbers containing these two numbers in the order they were received.
Finally, create an empty array called $emptyList.
To print the contents of an array as a comma-separated string, use implode(). It takes a separator string and an array, and joins all elements together:
implode(",", $fruits) → Apple,Banana,Cherry
Print the following on separate lines:
- The word
Fruits:followed by a space and the three fruits separated by commas (no spaces after commas) - The word
Numbers:followed by a space and the two numbers separated by commas (no spaces after commas) - The word
Empty:followed by a space and eitheryesif the empty array has no elements, ornootherwise (use a ternary operator withcount()to check if the array is empty)
Example:
If the inputs are Apple, Banana, Cherry, 10, and 20, the output should be:
Fruits: Apple,Banana,Cherry
Numbers: 10,20
Empty: yesIf the inputs are Mango, Orange, Grape, 5, and 15, the output should be:
Fruits: Mango,Orange,Grape
Numbers: 5,15
Empty: yesCheat sheet
PHP provides two ways to create indexed arrays:
Using square brackets (modern syntax):
<?php
$colors = ["Red", "Green", "Blue"];
?>Using the array() function:
<?php
$colors = array("Red", "Green", "Blue");
?>Arrays can hold any type of data:
<?php
$numbers = [10, 20, 30, 40];
$mixed = ["Hello", 42, true];
?>Creating an empty array:
<?php
$tasks = [];
?>PHP automatically assigns index numbers starting from 0.
Try it yourself
<?php
// Read fruit inputs
$fruit1 = trim(fgets(STDIN));
$fruit2 = trim(fgets(STDIN));
$fruit3 = trim(fgets(STDIN));
// Read number inputs
$num1 = intval(fgets(STDIN));
$num2 = intval(fgets(STDIN));
// TODO: Write your code below
// 1. Create an indexed array called $fruits containing the three fruits
// 2. Create an indexed array called $numbers containing the two numbers
// 3. Create an empty array called $emptyList
// 4. Print the results as specified
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators6Arrays Part 1 - Indexed
Introduction to ArraysCreating Indexed ArraysAccessing Elements by IndexModifying Elements by IndexArray Size with CountAdding Elements to an ArrayRecap - Managing a Simple List