Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To modify an associative array in PHP so that its values become keys, you can use the array_flip() function. This function takes an array as input and returns a new array where the keys and values are flipped. Here is an example:

// original associative array
$fruits = array(
    'apple' => 'red',
    'banana' => 'yellow',
    'orange' => 'orange'
);

// flip array using array_flip()
$colors = array_flip($fruits);

// output new associative array with values as keys
print_r($colors);

This code will output:

Array
(
    [red] => apple
    [yellow] => banana
    [orange] => orange
)

As you can see, the keys and values have been flipped, so the values are now the keys in the new array.