LERAN PHP COMPLETE

Learn one of the most powerful programming languages in the world and become a rockstar developer.

PHP Arrays


An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

There are three different kind of arrays and each array value is accessed using an ID c which is called array index.

  • Indexed array − An array with a numeric index. Values are stored and accessed in linear fashion.

  • Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.

  • Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices

Create an Array in PHP

In PHP, the array() function is used to create an array:

array();

Indexed array

There are two types to create indexed arrays.

First method automatically assign the value starting from 0.

$adzetech = array("Ram", "Shyam", "Hari", "Suresh");

Next method is to assign index manually.

$adzetech[0] = "Ram";
$adzetech[1] = "Shyam";
$adzetech[2] = "Hari";
$adzetech[3] = "Suresh";
<?php
     $adzetech = array("Ram", "Shyam", "Hari", "Suresh");
     echo "I Love " . $adzetech[0] . ", " . $adzetech[1] . ", " . $adzetech[2] . " and " . $adzetech[3] . ".";
?>

Get The Length of an Array - The count() Functionh

<?php
    $adzetech = array("Ram", "Shyam", "Hari", "Suresh");
    echo count($adzetech);
?>

Output : 4

Loop Through an Indexed Array

<?php
    $adzetech = array("Ram", "Shyam", "Hari", "Suresh");
    $arrlength = count($adzetech);
    for($x = 0; $x < $arrlength; $x++) {
        echo $adzetech[$x];
        echo "<br>";
    }
?>

Associative array

There are two ways to create associate array:

$marks = array("Suresh"=>"95", "Hari"=>"65", "Joe"=>"43");

OR

$marks['Suresh'] = "95";
$marks['Hari'] = "65";
$marks['Joe'] = "43";
<?php
    $marks = array("Suresh"=>"95", "Hari"=>"65", "Joe"=>"43");
    echo "Suresh scored " . $marks['Suresh'] . " marks.";
?>

Loop Through an Associative Array

<?php
    $marks = array("Suresh"=>"95", "Hari"=>"65", "Joe"=>"43");
    foreach($marks as $x => $x_value) {
        echo "Name=" . $x . ", Marks=" . $x_value;
        echo "<br>";
    }
?>

PHP Multidimensional Arrays

In above tutorial, we have described arrays that are a single list of key/value pairs.

However, sometimes you want to store values with more than one key.

This can be stored in multidimensional arrays.


Note: The dimension of an array indicates the number of indices you need to select an element.

  • For a two-dimensional array you need two indices to select an element
  • For a three-dimensional array you need three indices to select an element

<html>
    <body>
    
        <?php
            $marks = array( 
                    "mohammad" => array (
                            "physics" => 35,
                            "maths" => 30,
                            "chemistry" => 39
                    ),
                    
                    "qadir" => array (
                            "physics" => 30,
                            "maths" => 32,
                            "chemistry" => 29
                    ),
                    
                    "zara" => array (
                            "physics" => 31,
                            "maths" => 22,
                            "chemistry" => 39
                    )
            );
            
            /* Accessing multi-dimensional array values */
            echo "Marks for mohammad in physics : " ;
            echo $marks['mohammad']['physics'] . "<br />";
            echo "Marks for qadir in maths : ";
            echo $marks['qadir']['maths'] . "<br />"; 
            echo "Marks for zara in chemistry : " ;
            echo $marks['zara']['chemistry'] . "<br />"; 
        ?>
    </body>
</html>

x
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine