PHP Arrays

Hi everyone,

An array is a variable that can hold multiple values inside itself.

For example: If we want to declare two fruit variables like below it is OK.

$fruit1="apple";
$fruit2="banana";

What if we want to hold all of the fruits. At this point arrays comes and helps us. We can access the values by referring to the array name.

Each element in the array has its own index so that it can be easily accessed.

There are 3 types of arrays in PHP:

  • Numeric array: An array with a numeric index
  • Associative array: An array where each ID key is associated with a value
  • Multidimensional array: An array containing one or more arrays

Numeric array

Each array element with a numeric index.

Let’s declare an array that holds some fruit names:

$fruits=array("apple", "banana", "orange");

We can declare the same array by writing:

$fruits[0]="apple";
$fruits[1]="banana";
$fruits[2]="orange";

Let’s do an example and call an array elements:

output will be:

banana is the most delicious fruit I like
apple and orange are also delicious fruits 

<?php
$fruits = array(
    "apple",
    "banana",
    "orange"
);
echo $fruits[1] . " is the most delicious fruit I like";
echo "</br>";
echo $fruits[0] . " and " . $fruits[2] . " are also delicious fruits";
?>

Associative array

In an associative array, each ID key is associated with a value. With associative arrays we can use the values as keys and assign values to them. Sometimes this arrays are the best to chose when we are programming PHP.

Let’s declare an associative array that has the student numbers as ID and student names as value:

output will be John

<?php
$students = array(
    142 => "John",
    155 => "Daniel",
    765 => "Gabriella"
);
echo $students[142];
?>

or we could do the same thing writing:

<?php
$students[142] = "John";
$students[155] = "Daniel";
$students[765] = "Gabriella";
echo $students[142];
?>

Multidimensional array

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

Here goes an example:

output will be:

Renault Megane
Renault Broadway
Peugeot Partner
Peugeot 307
Hyundai Accent
Hyundai Genesis

<?php // php arrays 4
$cars = array(
    "Renault" => array(
        "Megane",
        "Broadway"
    ),
    "Peugeot" => array(
        "Partner",
        "307"
    ),
    "Hyundai" => array(
        "Accent",
        "Genesis"
    )
);
foreach ($cars as $key1 => $dim1)
    foreach ($dim1 as $key2 => $dim2)
        echo $key1 . " " . $dim2 . "</br>";
?>

Note: I didn’t mention about foreach loop yet. I am gonna talk about it later. So, for now don’t worry. Just focus on understanding multidimensional array stuff 😉

To get a single value from multidimensional array:

output will be:

Is Megane a Renault product? 

echo "Is " . $cars["Renault"][0] . " a Renault product?";

In this lesson you’ve learned about PHP arrays. To learn more, stay connected! 😉

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *