Associative Arrays in PHP

2
573
Associative Arrays in PHP

A new Article in the series of PHP programs for beginners guys, excited? We have posted a number of articles to Learn PHP Programming here, today we will talk about something called Associative arrays.When we first started learning about arrays in PHP, we saw an introduction to arrays article. Where we talked about various types of arrays like – Numerically Indexed Arrays, Associative Arrays and Multi-Dimensional Arrays in PHP. Recently we posted a tutorial on Creating Indexed Arrays in 3 Ways. Today we will learn about the Associative Arrays in PHP, how can you create them and use them. So let’s start our lesson,

What are Associative Arrays in PHP?

The first type of array is of Numerically Indexed arrays, where we access values stored in an array by their index postions. Suppose you have to store marks of 500 students in an array, now to access marks of a particular student, you have to remember their Index Postion. It will create unneccesary problems and lag.

In Associative arrays we can access values stored in an array using their key. Don’t get confused. Lets make it more easy. In Associative arrays we store a pair of values together (that is, pair of key and value). Each value is accessed using their key. Just like a lock of your home is opened using key of that lock. Here we can access each value using their keys. Now we can store marks of Mr. X and Mr. Y, and don’t have to remember at which index postion we stored them. To access marks of X and Y student, we will simply use their key. Analyze the code below and try to understand the concept.

<?php

//First method to Create associatve arrays in PHP

echo “<br>***First***<br>”;
$marks = array(
‘manish’=>33,
‘Mohit’=>38,
‘Jacob’=>40
);
print_r($marks);

/*Here Manish, Mohit and Jacob are keys and 33,38,40 are values of their respective keys. Print_r() function prints all the values of an array*/

//second method to create associative arrays

echo “<br>***SECOND***<br>”;
$student[‘name’]=”macky”;
$student[‘marks’]=40;

print_r($student);

echo “<br>***Printing an Array using foreach Loop***<br>”;

$city=array(‘Del’=>”Delhi”, ‘Mum’=>”Mumbai”,’uk’=>”Uttarakhand”);
foreach($city as $k=>$v)
echo “<BR>$k — $v “;

/*foreach arrays can be used to print all values of an associative array in PHP. We simply write foreach then array name in parenthesis and after ‘as’ keyword… $k=>$v where $k means keys and $v means values*/

echo “<br>***Another Array printing***<br>”;

foreach($marks as $k=>$v)
echo “<br>$k has got $v marks”;

?>

Associative Arrays in PHP
Output for above Example Code in PHP

Hope you liked our article on Associative Arrays in PHP, if you have any doubt regarding this, submit your query by commenting down below or by requesting any program from here. Our Next Articles will be – Example Programs for Arrays in PHP and Multi-Dimensional Arrays. So without wasting any time, join our newsletter now to get updates. Keep Learning.

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here