Q:

How to get all the values from an associative array in PHP

0

How to get all the values from an associative array in PHP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the PHP array_values() function

You can use the PHP array_values() function to get all the values of an associative array.

Let's try out an example to understand how this function works:

<?php
$cities = array("France"=>"Paris", "India"=>"Mumbai", "UK"=>"London", "USA"=>"New York");
 
// Get values from cities array
print_r(array_values($cities));
?>

You can also use the PHP foreach loop to find or display all the values, like this:

<?php
$cities = array("France"=>"Paris", "India"=>"Mumbai", "UK"=>"London", "USA"=>"New York");
 
// Loop through cities array
foreach($cities as $key => $value){
    echo $key . " : " . $value . "<br>";
}
?>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP  and MySQL Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to sort an associative array by key in PHP... >>
<< How to get all the keys of an associative array in...