PHP Associative Arrays

Associative Arrays in PHP store data in key-value pairs. Unlike numerically indexed arrays, you can index each element with a label or a key. Keys are easy to remember. For example, you can store structured data easily in an associative array. In this article, we will discuss PHP Associative Arrays. Also, we will discuss ways for creating, inserting and accessing elements in an associative array.

Creating Associative Array in PHP

It is easy to create Associative Arrays in PHP. Suppose we want to create an array to store a score of students in an array. It is better to store it with the name of the student as key and the score as value.

<?php
	
	$scoreArray = array(
		'Chandler' => 50, 
		'Monica' => 80, 
		'Ross' => 95
	); 

?>

Note that:

  • $scoreArray is the name of the variable.
  • [‘KeyName’] is the index key of the element.
  • The integer value is the score of that student.
PHP associative arrays diagram

Inserting in Associative Array in PHP

You can insert new elements in an associative array in PHP by using the assignment operator as follows:

<?php
	//Creating an Array
	$scoreArray = array(
		'Chandler' => 50, 
		'Monica' => 80, 
		'Ross' => 95
	); 

        //Inserting New Elements
	$scoreArray['Joey'] = 75;
	$scoreArray['Rachael'] = 55;

?>

In the above code snippet, we first initialise an array with some key-value pairs. Then we insert new elements by assigning the values to a key.

Note: You can also create an array by initialising an empty array and then inserting elements to it.

<?php
	
	$scoreArray = array(); 
	$scoreArray['Joey'] = 75;
	$scoreArray['Rachael'] = 55;

?>

Accessing Elements from Associative Array in PHP

You can access elements from a PHP Associative Array by simply referencing to the key.

<?php
	
	$scoreArray = array(
		'Chandler' => 50, 
		'Monica' => 80, 
		'Ross' => 95
	); 
	echo 'Chandler\'s Score: '.$scoreArray['Chandler'];
	echo 'Monica\'s Scored: '.$scoreArray['Monica'];
	echo 'Ross\'s Score: '.$scoreArray['Ross'];

?>
OUTPUT

Chandler's Score: 50
Monica's Scored: 80
Ross's Score: 95

Traversing the Associative Array

We can loop through PHP Associative Arrays using a for loop and a foreach loop.

For Loop

We need the total number of elements in the array for using the for loop. We can get it by using the PHP Count Function. Also, we need the keys of the array elements using PHP array_keys Function.

<?php
	
	$scoreArray = array(
		'Chandler' => 50, 
		'Monica' => 80, 
		'Ross' => 95
	); 
	$keysArray = array_keys($scoreArray);
	for ($counter = 0; $counter < count($scoreArray); $counter++) {
		$key = $keysArray[$counter];
		echo $key.'-->'.$scoreArray[$key];
	}

?>
OUTPUT:
Chandler-->50
Monica-->80
Ross-->95

However, this method is not very optimised as we require the use of count and array_keys function also. The foreach loop is a much better approach over the for loop.

Foreach Loop

<?php
	
	$scoreArray = array(
		'Chandler' => 50, 
		'Monica' => 80, 
		'Ross' => 95
	); 
	foreach($scoreArray as $key => $value) {
		echo $key.'-->'.$value;
	}

?>
OUTPUT:
Chandler-->50
Monica-->80
Ross-->95

Conclusion

In conclusion, we discussed PHP Associative Arrays in this article. It is one the most important concepts in PHP and most of the code is built around it. You can read more about them on the Official PHP Documentation. Also, you might be interested in reading about PHP Array Functions on Concatly.

Spread the Knowledge
Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *