Home Tutorial Php Phpbasics PHP Associative Array

 
 

Share on Google+Share on Google+
PHP Associative Array
Posted on: October 21, 2009 at 12:00 AM
Advertisement
PHP Associative Array: This tutorial is about associative array, which is included in php. The Associative Array is quite different than numerically indexed arrays, this kind of arrays could have string or numeric keys. In this tutorial you also come to know about each() function, and list() function. This tutorial has variety of examples.

Associative Array

In numeric array, the index position is start from 0, the second is index position is 1 and so on. PHP supports associative array, in which we can associate any kind of key (generally string or numeric) with a value.

Use

Sometimes it is more beneficial to have String keys or indexes in an array instead of numeric keys. For instance if you want to store the salary of  employee or the marks obtained by the students, then it will be more beneficial to use associative array instead of using numeric array.

Initializing an Associative array

$array=array("City"=>"New Delhi", "State"=>"Delhi"," Pin"=>110029)

Like the numerically indexed array we can create and initialize an associative array with one element at a time. Following example will help you to understand this concept:

Example 1:

<?php

$array["Sedan"]="Manza";

$array['suv']="Safari";

$array['nano']=2009;

print_r($array);

?>

Output:

Array ( [Sedan] => Manza [suv] => Safari [nano] => 2009 )

Example 2 is similar to the Example 1 with a single  difference:

Example 2:

<?php

$array=array ("Sedan"=>"Manza");

$array['suv']="Safari";

$array['nano']=2009;

print_r($array);

?>

Output:

Array ( [Sedan] => Manza [suv] => Safari [nano] => 2009 )

One difference between numerically indexed array and associative array is that we can not retrieve the values of associative array using simple loops, we need to use foreach loop to access the values of associative array. if you want to learn more about foreach loop please visit our webpage: http://roseindia.net/tutorial/php/phpbasics/PHP-Addition-of-Array.html 

Example 3:

<?php

$array=array ("Sedan"=>"Manza");

$array['suv']="Safari";

$array['nano']=2009;

foreach($array as $val){

echo $val;

echo "<br/>";}

?>

Output:

Manza
Safari
2009

There is another way to use foreach loop, as follows:

Example 4:

<?php

$array=array ("Sedan"=>"Manza");

$array['suv']="Safari";

$array['nano']=2009;

foreach($array as $key=>$value){

echo $key.'=>'.$value.'<br/>';}

?>

Output:

Sedan=>Manza
suv=>Safari
nano=>2009

Another way to access the key and value of an array is using each language construct. each return the key and value pair of an array and increase the array index position. Small description of each is given below:

General Format array each(array $array)
Parameter an array
Return Value array

Example 5:

<?php

$array=array("Jaguar","Land Rover");

while($val = each($array)){

echo $val ['key'];

echo '=>';

echo $val['value']."<br/>";}

?>

Output:

0=>Jaguar
1=>Land Rover

In the above example we have used the each function in while loop, using the loop each function start from the first index position of the array and advance to the next position, and this task will continue until the last position is reached. As we know each() function returns the key and value, we need to store those and print accordingly.

Another way to print the associative arrays is using list. A small description of list is as follows:

General Format void list(mixed $var [, ..])
Parameters $var: variable
Return Value --------------------

Example 6:

<?php

$array=array('manza','vista','nano');

list($indigo, $indica, $new)=$array;

echo "New version of indigo is $indigo,"."<br/>";

echo "New version of indica is $indica and "."<br/>";

echo "New car from tata motors is $new";

?>

Output:

New version of indigo is manza,
New version of indica is vista and
New car from tata motors is nano

If you want to store only one value into the list variable then leave the respective $variable as follows:

Example 7:

<?php

$array=array('manza','vista','nano');

list(, , $new)=$array;

echo "New car from tata motors is $new";

?>

Output:

New car from tata motors is nano

If you want to display the key along with value using list function then you have to use each function as follows:

Example 8:

<?php

$car=array('manza','vista');

while(list($key,$ncar)=each($car)){

echo "$key=>$ncar<br/>";}

?>

Output:

0=>manza
1=>vista

Advertisement

Related Tags for PHP Associative Array:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 21, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com