PHP Arrays

PHP Arrays

An array is used to store multiple values in a single variable in PHP. In PHP array() function is used to create an array.

Types of Arrays in PHP

  • Indexed array — Array with numeric key.
  • Associative array — Array with name key.
  • Multidimensional array — Array containing one or more arrays within itself.

Indexed Arrays

An indexed array stores each array element with a numeric index.
Example

<?php
// Define an indexed array
$countries = array("USA", "UK", "INDIA"); 
?>

Associative Array

An Associative array stores each array element with a name index.

<?php
// Define an associative array
$capital = array("India"=>"delhi", "USA"=>"Washington", "Uk"=>"London");
?>

Multidimensional Array

Array containing one or more arrays within itself.

<?php
$contacts = array(
    array(
        "name" => "John",
        "city" => "USA",
    ),
    array(
        "name" => "Henary",
        "city" => "UK",
    ),
    array(
        "name" => "Raj",
        "city" => "India",
    )
);
?>