PHP Tutorial


PHP 7 Arrays Tutorial


PHP Arrays

An array is a type of special variable in PHP. It stores multiple elements/items of the same data type in a single variable. So that does not need to declare any other variable for each data. With this, it creates a list of items/elements of a similar data type that can be accessed using its index/key.

Note: You can define array in PHP by using PHP array function " array( ); " and square bracket " [ ] ".

Example: The following Example of An array in PHP.

$tutorial = array( "PHP", "PYTHON", "NODE JS");

OR

$tutorial = [ "PHP", "PYTHON", "NODE JS" ];

Let's create an array in PHP:

    <!DOCTYPE html>
    <html>
    <body>

    <?php
    $tutorial = [ "PHP", "PYTHON", "NODE JS" ];
    
    // Print array
    Print_r($tutorial);
    echo "<br>";

    //Access the array value by key
    echo $tutorial[0] ."<br>";
    
    echo $tutorial[1] ."<br>";
    
    echo $tutorial[2] ."<br>";

    ?>

    </body>
    </html>

According to Example, " $tutorial " it is a variable In which "PHP", "PYTHON", "NODE JS" elements are stored.

Parameter of An Array

1. Key Defines the key Numeric / String.
2. Value Defines the value.

Advantages of PHP array

  1. Less Code.
  2. Easy to traverse.
  3. Sorting.

There are three types of the array in PHP

  • PHP Indexed Arrays
  • PHP Associative Arrays
  • PHP Multidimensional Arrays

PHP Indexed Arrays

PHP Indexed Array is used to hold any type of item/element. In Array, index/key will be represented by a number. By default, PHP indexed array begins with 0. it is also known as a numeric array.

Syntax of PHP Indexed Array

    array(value1, value2, value3);

Examples: The following Examples for understanding PHP Indexed Arrays.

1. Access PHP Indexed Array

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $source = array("welcome","to","w3alert.com"); 

        echo $source[0] ." ". $source[1] ." ". $source[2] . ".";    // keys are 0,1,2

    ?>

    </body>
    </html>

2. PHP Indexed Array with for Loop

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $books = array( "Wings of Fire","The Secret","War and Peace");

    $arrlen=count($books); // count the array length

    for( $m = 0; $m < $arrlen; $m++ ) {

      echo $books[$m];
      echo "<br>";
    }

    ?>

    </body>
    </html>

PHP Associative Arrays

The associative array is similar to the indexed array. Associative arrays are held elements or values in key-value pairs.

The array whose keys reside in string form is called array association array.

Syntax of PHP Associative Array

    array("key1"=>"value1", "key2"=>"value2", "key"=>"value3");

Examples: The following Examples for understanding PHP Associative Arrays.

1. Access PHP Associative Array

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $laptop_price = array( "Dell Vostro" => "25995","HP Notebook" => "21290","Lenovo Ideapad" => "27990" );

    echo "The Price of Dell Vostro:"." ". $laptop_price['Dell Vostro'] ."<br>";

    echo "The Price of HP Notebook:"." ". $laptop_price['HP Notebook'] ."<br>";

    echo "The Price of Lenovo Ideapad:"." ". $laptop_price['Dell Vostro'] ."<br>";

    ?>

    </body>
    </html>

2. PHP Associative Array with for Loop

    <!DOCTYPE html>
    <html>
    <body>

    <?php
    $old_year = array("Gretel"=>"20","Elizabeth"=>"24");

    $keys = array_keys($old_year); 
    $arrlength = count($old_year);  
      
    for($l=0; $l < $arrlength; $l++) { 
        echo $keys[$l] . ' ' . $old_year[$keys[$l]] . "\n"; 
    } 
      
    ?> 

    </body>
    </html>

3. PHP Associative Array with forEach Loop

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $laptop_price = array( "Dell Vostro" => "25995","HP Notebook" => "21290","Lenovo ideapad" => "27990" );

    foreach($laptop_price as $m => $m_value) {

      echo "Key=" . $m . ", Value=" . $m_value;
      echo "<br>";
    }

    ?>
    
    </body>
    </html>

PHP Multidimensional Arrays

PHP Multidimensional array is used to store more than one arrays. Each element in the array holds the other sub-arrays. PHP Multidimensional array has multiple indexes to access the sub-array.

Syntax of PHP Multidimensional Array

    array (

        array ( value1, value2, value3 ),

        array ( value1, value2, value3 ),

    );

Examples: The following Examples for understanding PHP Multidimensional Arrays.

1. Access PHP Multidimensional Array 

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // create PHP Multidimensional Array

    $info = array
      (
      array("name"=>"John","age"=>"23","specialist"=>"PHP"),
      array("name"=>"Marco","age"=>"28","specialist"=>"PYTHON"),
      array("name"=>"Devin","age"=>"32","specialist"=>"PHP,JAVA,PYTHON"),
      );

    // for Sub-array

    print_r($info[0]);

    echo "<br>";

    print_r($info[1]);

    echo "<br>";

    print_r($info[2]);

    echo "<br>";

    // for Single Value

    print_r($info[0]["name"]);

    echo "<br>";

    print_r($info[1]["name"]);

    echo "<br>";

    print_r($info[2]["name"]);

    echo "<br>";

    ?>

    </body>
    </html>

2. PHP Multidimensional Array with forEach Loop

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $info = array
        ( 
            array("name"=>"John","age"=>"23","specialist"=>"PHP"),
            array("name"=>"Marco","age"=>"28","specialist"=>"PYTHON"),
            array("name"=>"Devin","age"=>"32","specialist"=>"PHP,JAVA,PYTHON"),
        );
        
        foreach($info as $key => $value){

            echo "<p><b> List $key Item</b></p>";
            echo "<ul>";

            foreach($value as $key1 => $value){

                echo "<li>" .$key1. " = " .$value. "</li>";
        }
          echo "</ul>";
        }

    ?>

    </body>
    </html>