Core PHP PHP

PHP Array

PHP Array

Array in php is an ordered of map, containing value on the basis of key or you can say that array is collection of data in group under single variable.

Suppose you want to store fruits name

Example

<?php

$fruits = array(“Apple”, “Banana”, “pineapple”, “Orange”);

?>

PHP Array Types

There are 3 types of array in PHP.

  1. Indexed / Numeric Array
  2. Associative Array
  3. Multidimensional Array

PHP Indexed Array

An indexed or numeric array stores each array element with a number index which starts from 0.

Example 1

<?php

$fruits = array(“Apple”, “Banana”, “pineapple”, “Orange”);

?>

Example 2

$ fruits[0]=” Apple”;

$ fruits[1]=” Banana”;

$ fruits[2]=” pineapple”;

$ fruits[3]=” Orange”;

 

PHP Associative Array

In associative array we are using key value pair. The assign to value, name with each elements using => sign.

Example 1

$salary=array(“Kamran”=>”350000″,”Amit”=>”450000″,”Sumit”=>”200000”);

Example 2

$salary[“Kamran”]=”350000″;

$salary[“Amit”]=”450000″;

$salary[“Sumit”]=”200000″;

 

PHP Multidimensional Array

Array of arrays. In php multidimensional array is an array in which each element can also be an array and each element in the sub-array, sub-array can be an array or further contain array within itself and so on.

<?php

$contacts = array(

array(

“name” => “sumit”,

“email” => “sumit@xyz.com”,

),

array(

“name” => “amit”,

“email” => “amit@xyz.com”,

),

array(

“name” => “kamran”,

“email” => “kamran@xyz.com”,

)

);

?>

 

Leave a Reply

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