Whatis Array in php?

An array is a special variable which can hold more than one value at a time

In php, the array( ) function is used to create an array.
Eg. array( );

Types of Array :-

There are three types of array.

  • 1. numeric array (indexed arrays)
  • 2. associative array.
  • 3. multidimensional array.
  • Numeric array (indexed array) :-

    :- numeric arrays can store numbers, Strings, and any object but their index will be prasented by numbers. By default array index starts from zero.

    Traverse array using foreach loop

    Example : First

    <?Php

    $data = array(1,2,3,4,5);
    foreach($data as $num)
    {
    echo "value is $num <br/>";
    }

    Output :

    value is 1
    value is 2
    value is 3
    value is 4
    value is 5

    Traverse array using for loop

    <?Php

    $data = array(1,2,3,4,5);
    $size = count($data);
    // using the for loop
    for($i = 0; $i < $size; $i++)
    {
    echo $data[$i], "\n";
    }

    Example : Second

    <?php

    $numbers[0] = "one";
    $numbers[1] = "Two";
    $numbers[2] = 3;
    foreach($numbers as $value)
    {
    echo "value is $value <br/>";
    }
    ?>

    Output :

    value is one
    value is Two
    value is 3

    Example :

    <? Php
    $books = array("java","laravel","php");
    echo "I like".$books[0].",".$books[1].",and".$books[2].".";
    ?>
    <br>
    <? Php
    $num = array(1,2,3,4,5);
    echo "I like".$num[0].",".$num[1].",and".$num[2].".";
    ?>


    Output :

    i like java,.net,php
    i like 1,2,3

    Example :

    <?php
    $seven = 7;
    $arrayname = array( "this is an element", 5, $seven );
    echo $arrayname[0];//prints: this is an element
    echo $arrayname[1]; //prints: 5
    echo $arrayname[2]; //prints: 7
    ?>

    Associative array :-

    The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index.
    Associative array will have their index as string so that you can establish a strong association between key and values.
    Associative arrays are arrays that use named keys that you assign to them.
    Note :- Don?t keep associative array inside double quote while printing otherwise it would not return any value.

    in associate array, we use => symbol.

    There are two ways to define associative array:

    Example :

    $salary=array("Mahtab"=>"350000","Habib"=>"450000","Kalam"=>"200000");

    Example : Second way

    $salary["Mahtab"]="350000";
    $salary["Habib"]="450000";
    $salary["Kalam"]="200000";

    Example :

    <?php
    $sal = array("mahtab"=> 10000, "alam"=>12000);
    echo "salary of mahtab is " .$sal['mahtab']."<br/>";
    echo "salary of alam is " .$sal['alam']."<br/>";
    ?>

    Output :

    salary of mahtab is 10000
    salary of alam is 12000

    Traversing PHP Associative Array

    Example : Using Foreach Loop.

    using foreach to traverse an associative array

    <?php
    $age = array("mahtab"=>"28", "alam"=>"26");
    foreach($age as $k => $value)
    {
    echo "key=" .$k . ", value = " .$value;
    echo "<br/>";
    }
    ?>

    Example : Using foreach Loop.

    <?php
    $arr = array("mahtab", "habib", "simran");
    foreach($arr as $key => $value)
    {
    echo $value ."=". $key ."\n";
    }

    ?>

    Example : Using for Loop.

    <?php
    $data = array("simran", "sana", "sara");
    $size = count($data);
    // getting the array of keys/index strings
    $keys = array_keys($data);
    for($i = 0; $i < $size; $i++)
    {
    echo $data[$keys[$i]] ."=". $keys[$i] ."\n";
    }

    ?>

    PHP Multidimensional Array

    Values in the multidimensional array are accessed using multiple index.

    A multidimensional array is an array that contains at least one other array as the value of one of the indexes.

    It allows you to store tabular data in an array. PHP multidimensional array can be represented in the form of matrix which is represented by row * column.

    Example :

    <?php
    $emp = array
    (
    array(1,"mahtab",200000),
    array(2,"habib",60000),
    array(3,"kalam",80000)
    );

    for ($row = 0; $row < 3; $row++) {
    for ($col = 0; $col < 3; $col++) {
    echo $emp[$row][$col]." ";
    }
    echo "<br/>";
    }
    ?>

    Example :

    <?php
    $marks = array(?mahtab? => array(?phy? => 40, ?che? => 35, ?math?=>50), ?alam?=> array(?phy? => 30, ?che? => 32, ?math?=>60));
    echo ?marks for mahtab in physics :?;
    echo $marks[?mahtab?][?phy?].?<br/>?;
    echo ?marks for alam in math :?;
    echo $marks[?alam?][?math?].?<br/>?;
    ?>

    Traversing PHP Associative Array

    <?php
    $salary=array("Mahtab"=>"550000","Asif"=>"250000","Alka"=>"200000");
    foreach($salary as $k => $v) {
    echo "Key: ".$k." Value: ".$v."<br/>";
    }
    ?>





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here