PHP - How to create an array  screenshot

PHP - How to create an array

An array is actually a map, because each key is mapped to a value. Arrays are very useful for storing data in cases when a variable can have many values. The array() function is used to create an array.

In PHP there are two ways to create array:

1. By passing the value in array function

<?php
$arr = array(Bobby”,
    “John”,
    “David);
?>

Also we can create array this way:

<?php
$arr = array(Bobby”,
    John”,
    “David”,
    array(US”,
        “Delhi”,
        “Europe);
?>

2. By creating index

<?php
$arr[] = “Bobby”;
$arr[] = “John”;
$arr[] = “David”;
?>

In PHP three types of array are used:

  • Indexed array
  • Associative array
  • Multidimensional array

Indexed array: An Indexed array is a just an array which is created in an index. There are two ways to create indexed arrays:

Here I am creating an array with three elements:

<?php
$arr = array(Peter”,
    “James”,
    “Symonds);
Print_r($arr);
?>

Associative array: This is an array with a key which is associated with each value. In an associative array we can use a string as the index of the array.

<?php
$student = array(name” => “Amittondon”,
    “address” => “London”,
    “course” => “B . E);
Print_r($student);
?>

Multidimensional array: This is an array which can contain another array as a value, so it is called a multidimensional array.

<?php
// A two-dimensional array:
$cars = array(
    array(
        "Volvo",
        100,
        96
    ) ,
    array(
        "BMW",
        60,
        59
    ) ,
    array(
        "Toyota",
        110,
        100
    )
);
?>

Example for an Indexed array:

<?php
echo "Index Array";
$arr = array(
    "Peter",
    "James",
    "Symonds"
);
Print_r($arr);
$array[] = "Peter";
$array[] = "James";
$array[] = "Symonds";
Print_r($array);
?>

Output:

<?php
IndexArray Array(
    [0] => Peter[1] => James[2] => Symonds
) Array(
    [0] => Peter[1] => James[2] => Symonds
)
?>

Example for an Associative array:

<?php
echo "Associative Array";
$array1 = array(
    "name" => "Amit tondon",
    "address" => "London",
    "course" => "B.E"
);
print_r($array1);
echo "";
foreach ($array1 as $key => $value) {
    echo "array1[$key] = $value";
}
echo "";
?>

Output:

<?php
AssociativeArray Array(
    [name] => Amittondon[address] => London[course] => B . E
) array1[name] = Amittondonarray1[address] = Londonarray1[course] = B . E
?>

Example for a Multidimensional array:

<?php
echoAtwo - dimensionalarray”;
$bikes = array(
    array(
        "Honda",
        100,
        96
    ) ,
    array(
        "Scooty",
        60,
        59
    ) ,
    array(
        "Maestro",
        110,
        100
    )
);
echo $bikes[0][0] . ": Ordered: " . $bikes[0][1] . ". Sold: " . $bikes[0][2] . "";
echo $bikes[1][0] . ": Ordered: " . $bikes[1][1] . ". Sold: " . $bikes[1][2] . "";
echo $bikes[2][0] . ": Ordered: " . $bikes[2][1] . ". Sold: " . $bikes[2][2] . "";
?>

Output:

<?php
Atwo - dimensionalarray Honda:
    Ordered:
        100.Sold:
            96Scooty:
                Ordered:
                    60.Sold:
                        59Maestro:
                            Ordered:
                                110.Sold:
                                    100
?>