To extract unique values from an array in PHP, you can make use of the array_unique()
function. Here's an example:
// Sample array
$array = array(2, 4, 6, 8, 2, 4, 10, 8);
// Extracting unique values
$uniqueArray = array_unique($array);
// Outputting unique values
print_r($uniqueArray);
Output:
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[6] => 10
)
In this example, the array_unique()
function removes the duplicate values from the given array and returns an array containing only the unique values.