Array In PHP


[1]. PHP uses three kinds of array:

Numeric 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.


Numeric Array Ex:

    $numbers = array( 1, 2, 3, 4, 5);

Associative Array Ex:

   $salaries = array(
        "mohammad" => 2000,
        "qadir" => 1000,
        "zara" => 500
   );

Multidimensional Array Ex:

   $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
    )
 );

[2].  Basically there are three Usage patterns of array in PHP.
Indexed array: Arrays with sequential numeric index, such as 0, 1, 2, etc. Example:


    $myarray = array();
    $myarray[0] = "test data 1";
    $myarray[1] = "test data 2";
    $myarray[3] = "test data 3";

Associative array: This is the most frequently used type of PHP arrays
whose elements are defined in key/value pair. Example:

    $myarray = array();
    $myarray["key1"] = "value 1";
    $myarray["key2"] = "value 2";
    $myarray["key3"] = "value 3";

Multidimensional array: Arrays whose elements may contains one or more arrays. There is no limit in the level of dimensions. Example:

    $myarray = array();
    $myarray[0] = array("data01","data02","data03");
    $myarray[1] = array("data11","data12","data13");

Comments

Popular posts from this blog

Java : Variables Declaring

Install DNF in RHEL/CentOS 7

SQL Self JOIN