PHP: Array and Loops

·

3 min read

An array is the collection of data that is stored sequentially. PHP can support an array of the same or different data types.

Indexed Array

The indexed array is similar to arrays in many other programming languages.

$array1 = [1, 2, 3, 4, 5];               // Array Definition
$array2 = ["Ram", "Shyam", "Hari"];     
// Output: 1
echo $array1[0];                         // Access array element  
// [1, 2, 3, 4, 5]
print_r($array1);                        // Print entire array

Array methods include count(), array_push(), array_pop(), array_merge(), etc.

// Prints Array Length. Output: 5
echo count($array1);     
$array_push($array1, 6);
// [1, 2, 3, 4, 5 ,6]
print_r($array1);       
$array_pop($array1);
// [1, 2, 3, 4, 5]
print_r($array1);      

$array3 = array_merge($array1, $array2);
// [1, 2, 3, 4, 5, "Ram", "Shyam", "Hari"]
print_r($array3);

There is a method named 'explode()' which can convert a formatted string into an array. It takes a separator string and the original string as arguments. Then, the original string is split according to the separator and an array of strings is generated.

$name = "Giver Khadka";
$separate = explode(" ", $name);
// Output: ["Giver", "Khadka"]
print_r($separate);

Associative Array

It stores arrays of key: value pairs and 'key' is used to access the 'value'. The array definition syntax is:

$array = ["key1"=>"value1", "key2"=>"value2", ...];

$array1 = 
[
    "fName"=>"Giver", 
    "lName"=>"Khadka",
    123=>"ID"
];
// Output: Giver
echo $array1["fName"];
// Output: ID
echo $array1[123];
// Push new element in array
$array1[456] = "SN";
// ["fName"=>"Giver", "lName"=>"Khadka", 0=>"ID", 1=>"SN"]
print_r($array1);

NOTE: When a 'key' is a number, we can use that number like an array index to access the 'value'. But, 'print_r($array1)' always starts printing the numerical 'key' from 0 to N.

Multidimensional Array

The syntax for accessing the elements of a 2D Indexed array is:

$array[row][col]

The syntax for accessing the elements of a 2D Associative array is:

$array[row][key]

// 2D Indexed Array Definition
$array1 = [
    [1, 2, 3],
    [4, 5, 6],
];
// Output: 6
echo $array1[1][2];
// Pushing new elements(array)
array_push($array1, [7, 8, 9]);
print_r($array1);

// 2D Associative Array Definition 
$array2 = [
    ["fName"=>"Ram", "lName"=>"Chandra", "age"=>32],
    ["fName"=>"Shyam", "lName"=>"Malla", "age"=>28],
];
// Output: 28
echo $array2[1]["age"];
// Pushing new elements(array)
array_push($array2, ["fName"=>"Hari", "lName"=>"KC", "age"=>30]);
print_r($array2);

For Loop

For loop iterates up to a specified range. So, it's used when the range is known.

$array = [1, 2, 3];
// Output: 1 2 3
for($i = 0; $i < count($array); $i++)
{
    echo "Hello World";
}

ForEach Loop

When the range or array length is not known. ForEach loop is used to iterate over each array element.

$array = [1, 2, 3];
// Output: 1 2 3
foreach($array1 as $item)
{
    echo $item;
}

While Loop

While loop checks the condition and runs the statement until the condition is satisfied.

$i = 0;
// Output: Hello Hello Hello 
while($i < 3)
{
    echo "Hello";
    $i++;
}

Do-While Loop

Do-While loop first runs the statement inside it and then checks the condition.

$i = 0;
// Output: World World World 
do
{
    echo "World";
    $i++;
}while($i < 5);