In PHP 8.4 & PHP 8.3, you can remove duplicate elements from an array using the `array_unique()` function. This function takes an array as input and returns a new array with duplicate values removed.
Here’s how you can do it in PHP 8.4 & PHP 8.3:
<?php // Sample array with duplicate elements $array = [1, 2, 2, 3, 4, 4, 5]; // Remove duplicate elements $uniqueArray = array_unique($array); // Print the resulting array print_r($uniqueArray); ?>
Output:
Array ( [0] => 1 [1] => 2 [3] => 3 [4] => 4 [6] => 5 )
Explanation:
* `array_unique()` removes duplicate values from the array.
* The keys of the original array are preserved. If you want to reindex the array, you can use `array_values()`:
<?php // Reindex the array $reindexedArray = array_values($uniqueArray); // Print the reindexed array print_r($reindexedArray); ?>
Output:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
This will give you an array with unique elements and reindexed keys.