Menu
Coddy logo textTech

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 icon

Challenge

Easy

Read 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:

  1. The word Fruits: followed by a space and the three fruits separated by commas (no spaces after commas)
  2. The word Numbers: followed by a space and the two numbers separated by commas (no spaces after commas)
  3. The word Empty: followed by a space and either yes if the empty array has no elements, or no otherwise (use a ternary operator with count() 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: yes

If the inputs are Mango, Orange, Grape, 5, and 15, the output should be:

Fruits: Mango,Orange,Grape
Numbers: 5,15
Empty: yes

Cheat 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

?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals