articles

Home / DeveloperSection / Articles / Array in PHP

Array in PHP

Anonymous User5996 08-Sep-2011

Array is collection of same data types. In PHP, an array stores multiple values in one single variable. I.e. an array is a special variable, which can store multiple values in one single variable.

In PHP, there are three types of array.

  1.    Numeric Array
  2.    Associative Array
  3.    Multidimensional Array
Numeric Array:

A numeric array stores each array element with a numeric index. i.e.  When we create numeric array, index automatically or manually assigned for each value of array at creation time.

There are two methods to create numeric array.

1.       Index automatically assigned( index starts at 0)

Syntax : $array_name = array (element1, element 2,……);

 

2.       Index manually assigned.

           Syntax:    $array_name[0] = element1;

               $array_name[1] = element2;

                ..........................

                ..........................

               $array_name[N] = elementN ; 

Let’s we have an example, how to implement numeric array in php.

Example:
<html>
<head>
       <title> Numeric Array</title>
       </head>
<body>
       <?php
              // Declare array variable with automatically index
              $array_auto =array (1,2,3,4,5,6,7,8,9);
 
              // Declare array varraible with manual index
              $array_manual [0] = "Arun";
              $array_manual [1] = "Kumar";
              $array_manual [2] = "Singh";
 
       // Print array with autmatically index
       print_r  ($array_auto) ;
 
       // print array with manual index
       print_r ($array_manual) ;
      
       ?>
</body>
 
</html>
Output:

Array in PHP

Note: ‘print_r’ is the function to print all values of array.

Associative Array:

An associative array, each ID key is associated with a value. When storing data about specific named values, a numerical array is not always the best way to do it. With associative arrays we can use the values as keys and assign values to them.

Syntax: Create associative array

$ass_array = array ( "indeId1" => value , "indeid1" => value, .........);

 Example: Let’s we have an example, how to implement Associative array in PHP.
<html>
<head>
       <title> Switch Statement</title>
       </head>
<body>
       <?php
              // Declare associative array variable
              $ass_array = array ("Arun" => 22, "Vineet" => 24, "Pandey" => 25);
 
              print_r ($ass_array);
             
       ?>
</body>
 
</html>
Output:

Array in PHP

Multidimensional Array:

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

Syntax:
 $multi_array = array (
    ele1 => array1
    (
      subele1,
      subele2,
      .......
      .......
      subeleN
    ),
    ele2 => array2
    (
      subele1,
      subele2,
      .......
      ........
 
    ),
    ........
    ........
    eleN = arrayN ()
    ) ;

 

Let’s we have an example, how to implement multidimensional array.

 Example:
<html>
       <head>  <title> Multidimensional Arrray Test </title>
       </head>
<body>
       <?php
              // Declare multidimensional array
              $multi_array = array(
                           "Author" => array (
                                  "Twash",
                                  "Brooker",
                                  "Hinggbotton"
                                  ) ,
                            "Subject" => array (
                                     "Math",
                                     "Physics",
                                     "Chemistry"
                                      ) ,
                            "School" => array (
                                    "Bal vikas",
                                    "Jaipuria",
                                    "Anandram"
                                  )
                           );
              // Print multidimensional array with print_r function
 
              print_r ($multi_array)     ;     
 
         ?>
 
</body>
</html>

 

Output:

Array in PHP

Example: Let’s we have an example, how to implement one-dimensional, two-dimensional and multi-dimensional array in php.

<html>
       <head>  <title> Array Test </title>
       </head>
<body>
<?php
// Declare  one- dimensional array
echo "<h3>"."This is one- dimensional array"."</h3>"."</br>";
$subject = array("Math","Physics","Chemistry","English","Hindi","Geography","Civics","History","Economics","Political Science");
             
// print one dimensional element using foreach loop
foreach ($subject as $value)
{
  echo ($value."</br>");
}
       
// Declare two-dimensional array
       $matrix  = array
                     ( array (1,2,3),
                       array (4,5,6),
                       array (7,8,9)
                     );
 
  echo "<h3>"."This is two dimensional array"."</h3>"."</br>";
// Print two dimensional array element
       for($row = 0 ; $row <3 ; $row++)
        {
           for($col = 0 ;$col <3 ;  $col++)
             {
                echo $matrix[$row][$col]."\t"."\t";
               }
               echo "</br>";
       }
// Declare multi-dimensional array
$organization = array (array (array ("Tech","Ankur","kumar"),
                             array  ("Sales","Arvind","javed"),
                             array  ("HR", "Bhabha","Phoole")
                             ),
                       array (array  ("Tech","Govind","Reimya"),
                             array  ("Sales","Guru","Chaurasia"),
                             array  ("HR", "Bhabha","Phoole")
                            )
                      );
echo "<h3>"."This is multidimensional Array"."</h3>"."</br>";
                    
for($layer = 0 ; $layer <2 ; $layer++)
 {
  for($row =0; $row<3;$row++)
   {
    for($col =0;$col<3 ;$col++)
       {
        echo $organization[$layer][$row][$col]."\t";
       }
  echo "</br>" ;
 }
 echo "</br>";
 }
 ?>
 
</body>
</html>
Output:

Array in PHP

 

 


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By