php

How to get all the keys of an associative array in PHP

In this tutorial, we are going to see how to get all the keys of an associative array in PHP. You can use the PHP array_keys() function to extract all the keys from an associative array.
 

How to get all the keys of an associative array in PHP
<?php
	$languages = array("p"=>"PHP", "j"=>"Java", "a"=>"Ada", "h"=>"HTML");

	print_r(array_keys($languages));
?>

Output:

Array ( 
	[0] => p 
	[1] => j 
	[2] => a 
	[3] => h 
)
You can also use the foreach loop to find or display all keys.

<?php
	$languages = array("p"=>"PHP", "j"=>"Java", "a"=>"Ada", "h"=>"HTML");
	
	foreach($languages as $key => $value){
		echo $key . " : " . $value . "<br>";
	}
?>

Output:

p : PHP
j : Java
a : Ada
h : HTML
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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