Quantcast
Channel: Programming Sphere » PHP array
Viewing all articles
Browse latest Browse all 3

Associative Array in PHP

$
0
0

PHP array is a memory management system where we can keep multiple data into same variable with different array index. Associative array is one kind of arrays in PHP. In my last article, I discussed about the basic structure of an array and how to declare and print array elements. I also discussed about the numeric array in PHP. Today I shall talk about associative array in PHP. If you are new here, please spend some times to study about array and numeric array in PHP. It will help you understand associative array so easily.

Associative array in PHP

Associative array in PHP

Associative array

If we represent array index as string value, it is called associative array in PHP programming language. See below example.

$a = array(‘id’=>1,’name’=>”Sayeed”,’age’=>24);

From the above example we can see that array indexes are declared as string value. So this type of array in PHP is called associative array.

How to print associative array’s elements

There are two ways to print elements of an associative array.

  1. Using index value directly.
  2. Using foreach loop statement.

Print associative array elements using index directly

We can print an associative array element using its index value directly. See below example.


<?php

$a = array('id'=>1,'name'=>"Sayeed",'age'=>24);

echo "ID = ".$a['id'];

echo "<br>Name = ".$a['name'];

echo "<br> Age = ".$a['age'];

?>

Output

ID = 1
Name = Sayeed
Age = 24

From the above example, we can easily see how to print associative array elements using index value directly. Here I kept index value into inverted comma but it is not mandatory if the index value be a single word. But if the index value be multiple word, we must use inverted comma otherwise PHP compiler will show an error message.

Print associative array elements using foreach loop statement

The easiest ways to print associative array elements using foreach loop statement. See below example where foreach loop is used to print associative array.


<?php

$a = array('id'=>1,'name'=>"Sayeed",'age'=>24);

foreach($a as $value){

echo $value."<br>";

}

?>

Output

1
Sayeed
24

From the example we can see that using foreach looping we can print associative array elements so easily. But from the output we can also see that printed result is not our desired output because there is no summery before the resulted value. Don’t worry. PHP foreach loop statement has an extra feature to show index value with key-value combination. That means, at the same time foreach loop statement can show both index value and element value. See below example.


<?php

$a = array('id'=>1,'name'=>"Sayeed",'age'=>24);

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

echo $key." = ".$value."<br>";

}

?>

Output  

id = 1
name = Sayeed
age = 24

With the key-value combination feature of foreach loop statement we are getting our desired result. So we can print both index value and element value at the same time with foreach loop statement.

Dear viewers, in this article I have discussed about associative array in PHP and how to print associative array using index value directly and using foreach loop statement. If you have any question or any suggestion, please discuss in comment section. I shall try my best to stay with you. In my next article I shall discuss about multiple or multidimensional array which is another most important type of type in PHP programming language.

 


Viewing all articles
Browse latest Browse all 3

Trending Articles