What are arrays in php?

Arrays in php are an ordered map which consists of keys and values. Keys can either be integer or strings.

Some of the facts of using different types as a key:

  • If string contains a number or float it will be cast as integer
  • Floats are casted to integers
  • bools are casted to integers
  • null will be casted to empty string

How to declare php array?

To declare php array you can use following simple syntax.

<?php

// numeric array
// list of fruits
$fruits = ['apple', 'oranges', 'banana'];

// display/debug what's in array
print_r($fruits);

// above command will output following
Array
(
    [0] => apple
    [1] => oranges
    [2] => banana
)

In above example, elements are assigned numeric key automatically as we did not define a key matching a value. An array in php always starts with index 0 and increments depending on size.

How to declare php array with keys?

To declare php array using keys and values you can use following example:

<?php

// list of cars
$cars = [
    'mazda'  => 'GS Sky',
    'toyota' => 'Corolla',
    'honda'  => 'Civic',
    'dodge'  => 'Caravan'
];

// print array keys/values
print_r($cars);

// output for above command
Array
(
    [mazda] => GS Sky
    [toyota] => Corolla
    [honda] => Civic
    [dodge] => Caravan
)

As you can see using keys it is easy to remeber the value. You will know soon as you look at the arrays. It is recommanded to provide name that make sense while declaring an array.

For example: if you are storing a car list in array use cars as name of the array.

How to declare multi-dimensional array in php?

Multi-dimensional arrays are nothing but arrays within an array. Let's have a look at following example:

<?php

// declaring multi-dimensional array
$multiDimensionalArray = [
    [0, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1, 1],
    [2, 2, 2, 2, 2, 2],
    [3, 3, 3, 3, 3, 3]
];

// debugging array
print_r($multiDimensionalArray);

// output of print_r
Array
(
    [0] => Array
    (
        [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 0
        )

    [1] => Array
(
    [0] => 1
            [1] => 1
            [2] => 1
            [3] => 1
            [4] => 1
            [5] => 1
        )

    [2] => Array
(
    [0] => 2
            [1] => 2
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => 2
        )

    [3] => Array
(
    [0] => 3
            [1] => 3
            [2] => 3
            [3] => 3
            [4] => 3
            [5] => 3
        )

)

Type casting with keys

As we learned earlier when defining keys with array we have to be very careful as it casts types other then integer to integer.

For example: floats, strings with numbers, bools are casted to integer.

Let's have a look at following example so that you understand.

<?php

// declaring an array
$array = [
    1    => "a",
    "1"  => "b",
    1.5  => "c",
    true => "d",
];

// debugging array
print_r($array);

// output for print_r
Array
(
    [1] => d
)

You will see that all the keys are casted to int therefore the value will be overwritten on every new element and the last assigned value "d" is the only one left over.

How to add values to php array?

Once we declare the array you can add more elements to an array using following example.

<?php

// declare fruits
$fruits = ['apple', 'orange'];

// add pear to fruits array
// empty bracket auto-increment index
$fruits[] = 'pear';

// debugging values in array
print_r($fruits);

// output
Array
(
    [0] => apple
    [1] => orange
    [2] => pear
)

// declare keys/values
$fruitList = array('fruit' => 'apple', 'veggie' => 'carrot');

// print single element of array
print $fruitList['fruit'];    // prints apple
print $fruitList['veggie'];   // prints carrot

// debugging values in array
print_r($fruitList);

// output
Array
(
    [fruit] => apple
    [veggie] => carrot
)

Let's have a look at some of the popular question that are being asked regarding php arrays.

Interview Questions

  • Php's numeric arrays begins with which position?
    • it begins with position 0
  • What are the types of array's in php?
    • Numeric (indexed)
    • Associative
    • Multi-dimensional

List of php array functions