PHP Arrays

Arrays store multiple values in a variable. For example, to store 10 numbers, you can define an array of length 10, instead of defining 10 variables. PHP arrays include Indexed Arrays, Associative Arrays, and Multi-dimensional Arrays.

In PHP, the array() function is used to create an array. Let’s see an example,

<!DOCTYPE html>
<html>

<body>

 <?php

     $cricketers= array("Sachin", "Virat", "Rohit");
     echo "Indian Cricketers: " . $cricketers[0] . ", " . $cricketers[1] ." and " .
     $cricketers[2] . ".";

?>


</body>
</html>

Here’s the output,

PHP arrays
As shown above, an array is created with the cricketer’s name and then printed with the echo output statement. Here, cricketer names are printed using array indexing i.e. $cricketers[0] for cricketer Sachin, $cricketers[0] for cricketer Virat, and $cricketers[0] for cricketer Rohit,

Now, we will learn about the types of arrays in PHP:

  • Indexed (Numeric) Arrays
  • Associative Arrays
  • Multidimensional Arrays

Indexed (Numeric) Arrays

The Indexed array consists of numeric index, so they are also called Numeric arrays. The array index is a number and the default index starts from 0.

Here’s an example, with an indexed array, topics. The array assigns two elements, which are the names of topics,

<!DOCTYPE html>
<html>
<body>

<?php

     $topics= array("Java", "Android");
     echo "Topics to learn this month: " . $topics[0] . ", " . $topics[1] . ".";

?>

</body>
</html>

Here’s the output,

PHP Indexed Arrays

Here, topic names are printed using array indexing i.e. $topics[0] for topic Java, and $topics[1] for topic Android,

Associative Arrays

These are arrays that have named keys associated with them. Here’s an example showing named keys associated with Java and Android,

<html>
<body>

 <?php

     $topics= array("Java" =>  15, "Android" => 20);
  
     echo "Days for learning Java: ".$topics['Java'] . "<br />";
     echo "Days for learning Android: ".$topics['Android'] . "<br />";

?>

</body>
</html>

Here’s the output,

PHP Associative Arrays

As shown above, the names keys associated with the array print the number of days for learning Java and Android respectively.

Multidimensional Arrays

An array with one or more than one array is known as a Multidimensional array. Two-dimensional arrays are multi-dimensional arrays and are also considered as an array of arrays.

Here’s an example of two-dimensional arrays, with associative arrays,

<!DOCTYPE html> 

<html>
<body>     

<?php

     $points = array(

        "viratkohli" => array (

             "testcricket" => 51,

             "ODI" => 53,         

             "T20" => 70
             ),

       "joeroot" => array (

             "testcricket" => 53,

             "ODI" => 37,

             "T20" => 38
            )
       );        

       echo "Points for Virat Kohli in ODI Cricket : " ;
       echo $points['viratkohli']['ODI'] . "<br />";


       echo "Points for Joe Root in T20 Cricket : ";
       echo $points['joeroot']['T20'] . "<br />";  
?>

</body>
</html>

Here’s the output,

PHP Multidimensional Arrays

Here, you can see two-dimensional arrays with names and keys associated with them i.e. associative arrays. It shows the points for two cricketers in all three formats i.e. testcricket, ODI, and T20. Using multi-dimensional arrays, we have shown it for 2 cricketers i.e. two-dimensional arrays.

Here, you can learn about another example. Now, we will print all the values, with an additional array for cricketer David Warner,

Test CricketODIT20
Virat Kohli515370
Joe Root533738
David Warner504245
<!DOCTYPE html>
<html>
<body>

<?php

     $points = array(

        "viratkohli" => array (

             "testcricket" => 51,

             "ODI" => 53,         

             "T20" => 70

             ),           

       "joeroot" => array (

             "testcricket" => 53,

             "ODI" => 37,

             "T20" => 38

            ),

       "davidwarner" => array (

             "testcricket" => 50,

             "ODI" => 42,

             "T20" => 45

            )

       );

       echo "<h2>Studyopedia Multi-Dimensional Array Example</h2>";

       echo "<h3>ODI Cricket</h3>";

       echo "Points for Virat Kohli in ODI Cricket : " ;

       echo $points['viratkohli']['ODI'] . "<br />";

        

       echo "Points for Joe Root in ODI Cricket : ";

       echo $points['joeroot']['ODI'] . "<br />"; 

      

       echo "Points for David Warner in ODI Cricket : " ;

       echo $points['davidwarner']['ODI'] . "<br /><br />";

        

       echo "<h3>T20 Cricket</h3>";

       echo "Points for Virat Kohli in T20 Cricket : ";

       echo $points['viratkohli']['T20'] . "<br />"; 

      

       echo "Points for Joe Root in T20 Cricket : " ;

       echo $points['joeroot']['T20'] . "<br />";

        

       echo "Points for David Warner in T20 Cricket : ";

       echo $points['davidwarner']['T20'] . "<br /><br />"; 

      

       echo "<h3>Test Cricket</h3>";

       echo "Points for Virat Kohli in Test Cricket : " ;

       echo $points['viratkohli']['testcricket'] . "<br />";

        

       echo "Points for Joe Root in Test Cricket : ";

       echo $points['joeroot']['testcricket'] . "<br />"; 

      

       echo "Points for David Warner in Test Cricket : ";

       echo $points['davidwarner']['testcricket'] . "<br />"; 

?>


</body>
</html>
PHP Variables
Run your first PHP program in XAMPP Server on localhost
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment