To extract all keys or values from an associative array in PHP, you can use the array_keys()
or array_values()
function respectively. Here's an example:
// An example associative array
$fruits = array(
"apple" => "red",
"banana" => "yellow",
"orange" => "orange"
);
// Extracting all the keys
$keys = array_keys($fruits);
// Extracting all the values
$values = array_values($fruits);
// Printing the keys and values
print_r($keys);
print_r($values);
Output:
Array
(
[0] => apple
[1] => banana
[2] => orange
)
Array
(
[0] => red
[1] => yellow
[2] => orange
)
In this example, array_keys()
is used to extract all the keys from the $fruits
array, and array_values()
is used to extract all the values. The resulting keys and values are then printed using print_r()
.