Easy way to create an array in PHP

In this tutorial, you will learn what is an array and how to create an array in PHP.

What is an array in PHP?

A PHP array is a container where you can store multiple values ​​at once.

It is like a PHP variable but, in a variable, you can store only one value at a time, where an array can hold one or more than one value.

How to create an array in PHP?

You can create a PHP array in two ways – 1) using the array() function, and 2) using the Square Brackets [].

Both methods do the same thing. The square bracket is the shorthand of the array() function.

<?php
$names = array('John', 'Smith', 'Mark', 'Ken', 'Barry');
<?php
$names = ['John', 'Smith', 'Mark', 'Ken', 'Barry'];
  • You can store values ​​of any data type in an array, such as string, number, boolean, etc.
  • It is not mandatory that all values ​​of an array be of the same data type.
<?php
$data = array('John', 21, true, 54.23);

print_r($data);
Array
(
    [0] => John 
    [1] => 21   
    [2] => 1    
    [3] => 54.23
)

How to access PHP array values?

You can access the values of a PHP array ​​by their keys. By default, each array has keys in a number format starting at 0. See the following image –

PHP array keys
PHP array keys
<?php
$names = array('John', 'Smith', 'Mark', 'Ken', 'Barry');

echo $names[0] . "\n"; // John
echo $names[3] . "\n"; // Ken
echo $names[4] . "\n"; // Barry
echo $names[2] . "\n"; // Mark
echo $names[1]; // Smith
John 
Ken  
Barry
Mark 
Smith

How to add custom keys to PHP array values?

With the help of the array operator =>, you can add custom keys to the values of a PHP array.

Create PHP array with custom keys
<?php
$user = array('name' => 'John', 'age' => 21, 'email' => '[email protected]');

echo $user['name'] . "\n";
echo $user['age'] . "\n";
echo $user['email'];
John
21
[email protected]

How many types of PHP arrays are there?

PHP has three types of arrays –

  1. Indexed arrays (Numeric arrays)
  2. Associative arrays
  3. Multidimensional Arrays

1. Indexed arrays (Numeric arrays)

Arrays with numeric keys are called Indexed or Numeric arrays.

<?php
$names = array('John', 'Smith', 'Mark', 'Ken', 'Barry');

2. Associative arrays

Arrays with custom keys are called Associative arrays.

<?php
$data = array('name' => 'John', 'age' => 21, 'email' => '[email protected]');

3. Multidimensional Arrays

When you create an array or arrays inside an array, it is called a multidimensional array, and these arrays can be Indexed or Associative.

<?php
$school = array(
    'teachers' => array('John', 'Mark', 'Smith'),
    'subject' => array('English', 'Science', 'Mathematics')
);
Example of Multidimensional array
<?php
$school = array(
    'teachers' => array(
        'english' => array('John', 'Mark', 'Smith'),
        'science' => array(
            'physics' => array('Hank', 'Joe'),
            'chemistry' => array('Akash', 'Niels'),
            'biology' => array('James', 'Theressa')
        ),
        'Mathematics' => array('Barry', 'Nick')
    )
);

$physicsTeacher = $school['teachers']['science']['physics'][0];

echo $physicsTeacher;
Hank

Leave a Reply

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