Home > Article > Backend Development > How to get different values in an array in php
In PHP, if we have an array containing multiple elements, how do we get the different values in it? This article will introduce some methods to achieve this goal.
1. Use the array_unique function
The array_unique function is a very useful function in PHP, which can get all different values from an array. We can understand how to use it with the following example:
$my_array = array('apple', 'banana', 'apple', 'pear', 'orange');
$unique_array = array_unique( $my_array);
print_r($unique_array);
Output: Array ( [0] => apple [1] => banana [3] => pear [4] => orange )
2. Use the array_count_values function
The array_count_values function can help us get the number of times each element in the array appears in the array. By parsing the resulting array we can easily get different values for the number of elements. For example:
$my_array = array('apple', 'banana', 'apple', 'pear', 'orange');
$count_array = array_count_values($my_array);
$ unique_count = count($count_array);
echo "There are ".$unique_count." different items.";
Output: There are 4 different items.
3. Use array_diff Function
array_diff function can be used to compare two arrays and return values that are present in the first array but not in the second array. If we use this function to compare a single array with itself, different values will be returned. For example:
$my_array = array('apple', 'banana', 'apple', 'pear', 'orange');
$unique_array = array_diff($my_array, array_unique($my_array) );
print_r($unique_array);
Output: Array ([1] => pear)
4. Using foreach loop
We can also use The foreach loop iterates through the array and uses a new array to hold the unique value of each element. For example:
$my_array = array('apple', 'banana', 'apple', 'pear', 'orange');
$unique_array = array();
foreach ($ my_array as $key => $value) {
if (!in_array($value, $unique_array)) { $unique_array[] = $value; }
}
print_r($unique_array);
Output: Array ( [0] => apple [1] => ; banana [3] => pear [4] => orange )
5. Using array_flip and array_keys functions
We can use the array_flip function to reverse the keys and values of the array, and then Use the array_keys function to get the keys of the replaced array. This will return the unique values of the original array. For example:
$my_array = array('apple', 'banana', 'apple', 'pear', 'orange');
$flip_array = array_flip($my_array);
$ unique_array = array_keys($flip_array);
print_r($unique_array);
Output: Array ( [0] => apple [1] => banana [2] => pear [3 ] => orange )
6. Use the array_reduce function
Finally, we can use the array_reduce function to reduce the array and return its unique value. This requires creating a callback function that checks if the element is contained in the new array. If not, add it to the array. For example:
$my_array = array('apple', 'banana', 'apple', 'pear', 'orange');
$unique_array = array_reduce($my_array, function ($result, $value) {
if (!in_array($value, $result)) { $result[] = $value; } return $result;
}, array());
print_r($unique_array);
Output: Array ( [0] => apple [1] => banana [3] => pear [4] => orange )
Conclusion
The above are several ways to get different values in an array in PHP. array_unique is the simplest and most commonly used method, but other methods are useful too, depending on your needs and application scenario. Please choose the solution that works best for you and use it when needed.
The above is the detailed content of How to get different values in an array in php. For more information, please follow other related articles on the PHP Chinese website!