Different methods for declaration of an arrays

In PHP, there are several ways to declare and initialize arrays:

1.	$arr1 = array();             // Empty array
2.	$arr2 = array(1, 2, 3);      // Indexed array with initial values
// Declares an indexed array with initial values 1, 2, and 3
// Accessing elements of the array 
echo $arr2[0]; // Outputs: 1 
echo $arr2[1]; // Outputs: 2 
echo $arr2[2]; // Outputs: 3
3.	$arr3 = array('a' => 1, 'b' => 2, 'c' => 3);  // Associative array with initial key-value pairs
The expression array('a' => 1, 'b' => 2, 'c' => 3); in PHP creates an associative array with three key-value pairs:
$array = array(
    'a' => 1,
    'b' => 2,
    'c' => 3
);
4.	$arr4 = array(array(1, 2), array(3, 4)); // Multidimensional array
In PHP, the line $arr4 = array(array(1, 2), array(3, 4)); declares a multidimensional array named $arr4. Let's break down what this means:

$arr4 = array(
    array(1, 2),   // Inner array 1 with elements 1 and 2
    array(3, 4)    // Inner array 2 with elements 3 and 4
);

 

In this multidimensional array:

  • $arr4 contains two elements, each of which is itself an array.
  • The first element ($arr4[0]) is an array containing 1 and 2.
  • The second element ($arr4[1]) is an array containing 3 and 4.

Also Read:-

Data Types in JavaScript

Bootstrap : Most popular CSS library in the World

Also Visit:-

https://inimisttech.com/

Leave a Reply