PHP Array

  • Hi guys ! In this tutorial we will learn about PHP Array .
  • Array is collection of values that have same data type . if you have list of diffrent fruits than the array named fruits will have elements like apple , banana , mango etc.
  • There are three types of array !
    • 1)Indexeed array
    • 2)Associative array
    • 3)Multidimensional array

Indexed array :

  • In indexed array value can be assigned to variable manualy and automatically .
  • In array every index start with 0

PHP Example

Edit it yourself

<!DOCTYPE html>
<html>
<body>

<?php
//automatically 
$cars = array("one","two","three");
print_r($cars);	

//manually 
$cars =  array();
$cars[0] = "one";
$cars[1] = "two";
$cars[3] = "three";
print_r($cars);

?>

</body>
</html>

Multidimensional Array :

  • A multidimensional array in PHP is an array that contains other arrays as its elements. It allows you to store and organize data in multiple dimensions, forming a grid-like structure.
  • Each element in a multidimensional array can itself be an array, creating a nested structure.
  • To create a multidimensional array in PHP, you can use square brackets to define the array and separate the elements with commas. Here's an example of a 2-dimensional array:

PHP Example

Edit it yourself

<!DOCTYPE html>
<html>
<body>

<?php
//automatically 
$matrix =  array("a"=>array("a1","a2","a3"),"b"=>array("b1","b2","b3"),"c"=>array("c1","c2","c3"));
print_r($matrix);

//manually 
$array = array([1,2,3],[4,5,6],[7,8,9]);
$array[0][1]."<br>";
$array[1][1]."<br>";
$array[2][1]."<br>";
$array[3][1];

?>

</body>
</html>