PHP Associative Array


 

PHP Associative Array

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.

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);

0

?>

Output:

1

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:

2

<?php

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

3

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

4

$array['nano']=2009;

foreach($array as $val){

5

echo $val;

echo "<br/>";}

?>

6

Output:

Manza
Safari
2009

7

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

Example 4:

<?php

8

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

9

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

$array['nano']=2009;

0

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

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

1

?>

Output:

2

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:

3

<?php

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

4

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

echo $val ['key'];

echo '=>';

5

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

?>

6

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.

7

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:

8

<?php

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

9

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

0

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

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

1

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

?>

2

Output:

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

3

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

Example 7:

4

<?php

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

5

list(, , $new)=$array;

6

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

?>

7

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:

8

Example 8:

<?php

9

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

0

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

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

?>

1

Output:

0=>manza
1=>vista

2

Ads