Array in php Reviewed by Momizat on . Array is a variable which is store more than one value. Array variable [php] <?php //Initialize $countries=array(); //Assign value $countries[]="India Array is a variable which is store more than one value. Array variable [php] <?php //Initialize $countries=array(); //Assign value $countries[]="India Rating: 0

Array in php

Array is a variable which is store more than one value.

Array variable

<?php
//Initialize 
$countries=array();

//Assign value

$countries[]="India";
$countries[]="USA";

$countries=array("India","USA");

// length of array using count() function

$length=count($countries);

//print all value of array variable using print_r() function

print_r($countries);
/*
output:- Array ( [0] => India [1] => USA ) 
*/
//Here $key is variable which store index or address of value help by (=>) operator and $countriesname is variable which store value of array

foreach($countries as $countriesname){
echo $countriesname."<br>";
}

foreach($countries as $key=>$countriesname){
echo $key."-".$countriesname."<br>";

}

//output
/*

0-India
1-USA
*/

//associative array

$countries=array('IN'=>'India','USA'=>'United States');
print_r($countries);
/*
Array ( [IN] => India [USA] => United States ) 
*/
//Multidimensional array

$countries=array('IN'=>array('MP'=>'Madhya Pradesh','UP'=>'Uttar Pradesh','HR'=>'Haryana'),'USA'=>array('FL'=>'Florida','AL'=>'Alabama','AZ'=>'Arizona','WA'=>'Washington','NY'=>'New York'));
print_r($countries);
/*
outpu:-
Array
(
    [IN] => Array
        (
            [MP] => Madhya Pradesh
            [UP] => Uttar Pradesh
            [HR] => Haryana
        )

    [USA] => Array
        (
            [FL] => Florida
            [AL] => Alabama
            [AZ] => Arizona
            [WA] => Washington
            [NY] => New York
        )

)

*/
?>

Leave a Comment

© 2014 Powered By