Arrays and its types in php ?

Array is the data structure that stores single of more types of data in single a single value or single variables . It is declared as

       var $array = array(‘hello” , “Welcome” , “to PHP ” , “Interview Questions”);

Basically in PHP there are three type of arrays :

  1. Indexed Arrays 
  • This means the arrays with the numeric index or keys .
  • It aromatically starts with 0 index or initialise 0 itself . 
As for example :
Let’s suppose an array 
var $array = array(‘Hello” , “Welcome” , “to PHP ” , “Interview Questions”);
So it can be manually assigned as 
$array[0] = “Hello”;
$array[1] = “Welcome”;
$array[2] = “to PHP”;
$array[3] = “Interview Questions”;
We can also print the array as :
 <?php echo $array[0] ; ?>
Output : Hello
 2. Associative Arrays 
  • Associative arrays are the arrays which uses the user defined names for as the keys.
As for example :
Let Consider an array :

$company = array(“John” => ” ABC Group” , “Rohan” => “BED Groups”);
So you can print as :

<?php  echo “John works in  ” . $company[‘John’] . ” as a manager .”; ?>

    Output : John works in ABC Group as a manager
3.Multidimensional Arrays 

Multidimensional arrays is the array which which stores one or many ( group of arrays ) at single place or on variables.

So lets go with an example :

Let suppose as a students in a class

$students = array   (
                             array(“Joe”,14,9),
                             array(“John”,15,10),
                             array(“Hari”,16,11),
                             array(“Lakhan”,17,12) 
                           );

So we can print is as :

<?php 
       echo $students[0][0].” is  “.$students[0][1].” years old and study in “.$students[0][2].”.<br>”; 
       echo $students[1][0].” is  “.$students[1][1].”years old and study in “.$students[1][2].”.<br>”; 
       echo $students[2][0].” is “.$students[2][1].”, years old and study in “.$students[2][2].”.<br>”; 
       echo $students[3][0].” is “.$students[3][1].”years old and study in “.$students[3][2].”.<br>”;
 ?>

Output :
            Joe is 14 years old and study in 9.
            John is 15 years old and study in 10.

            Hari is 16 years old and study in 11.
            Lakhan is 17 years old and study in 12.

For more about multidimensional and looping we shall discuss in next topic :

Follow the links for multidimensional : http://phpinterviewquestions.evernews.online/2018/02/what-is-multidimensional-arrays-in-php.html

Leave a Reply

Your email address will not be published.Required fields are marked *