PHP unique array by value?
basically
$unique_array = [];
foreach($your_array as $element) {
$hash = $element[field-that-should-be-unique];
$unique_array[$hash] = $element;
}
$result = array_values($unique_array);
How to remove duplicate values from an array in PHP
Use array_unique().
Example:
$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
How to find unique values in array
You can use array_unique()
in single line like below:-
<?php
$unique_array = array_unique($array); // get unique value from initial array
echo "<pre/>";print_r($unique_array); // print unique values array
?>
Output:- https://eval.in/601260
Reference:- http://php.net/manual/en/function.array-unique.php
If you want in string format:-
echo implode(',',$final_array);
Output:-https://eval.in/601261
The way you want is here:-
https://eval.in/601263
OR
https://eval.in/601279
unique array on base on value of specific key
You could simply loop through the array and add them to a new array, indexed by hotelID
. This way any duplicates will just overwrite the existing value and you end up with one entry per hotel:
$unique = array();
foreach ($hotels as $value)
{
$unique[$value['hotelID']] = $value;
}
$data['uniqueHotels'] = array_values($unique);
array_unique for objects?
Well, array_unique()
compares the string value of the elements:
Note: Two elements are considered equal if and only if
(string) $elem1 === (string) $elem2
i.e. when the string representation is the same, the first element will be used.
So make sure to implement the __toString()
method in your class and that it outputs the same value for equal roles, e.g.
class Role {
private $name;
//.....
public function __toString() {
return $this->name;
}
}
This would consider two roles as equal if they have the same name.
Need to get unique values in array
You can always do something like this:
$colorarray = array();
foreach($catIds as $catid){
if(!in_array($catid, $colorarray) {
$colorarray[] = $catid;
}
}
But also this has n*n complexity, So if your array is way too big, it might not be the most optimised solution for you.
How to remove duplicate values from a multi-dimensional array in PHP
Here is another way. No intermediate variables are saved.
We used this to de-duplicate results from a variety of overlapping queries.
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
How can you get unique values from multiple arrays using array_unique?
The answer to the first part of your question is NO.
The array_unique function definition in the PHP manual states that array_unique
takes exactly two arguments, one array, and an optional integer that determines the sorting behavior of the function.
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
Rather than take the manual's word for it, here are some test arrays.
$one_array = ['thing', 'another_thing', 'same_thing', 'same_thing'];
$two_arrays = ['A', 'B', 'C', 'thing', 'same_thing'];
$or_more_arrays = ['same_thing', 1, 2, 3];
A couple of test show that the function does work as advertised:
$try_it = array_unique($one_array);
returns ['thing', 'another_thing', 'same_thing'];
$try_it = array_unique($one_array, $two_arrays);
gives you a warning
Warning: array_unique() expects parameter 2 to be integer, array given
and returns null
.
$try_it = array_unique($one_array, $two_arrays, $or_more_arrays);
also gives you a warning
Warning: array_unique() expects at most 2 parameters, 3 given
and returns null
.
The answer to the second part of your question is YES.
To get unique values using array_unique, you do have to have one array of values. You can do this, as u_mulder commented, by using array_merge
to combine the various input arrays into one before using array_unique
.
$unique = array_unique(array_merge($one_array, $two_arrays, $or_more_arrays));
returns
['thing', 'another_thing', 'same_thing', 'A', 'B', 'C', 1, 2, 3];
If instead of several individual array variables, you have an array of arrays like this:
$multi_array_example = [
[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6]
];
Then you can unpack the outer array into array merge to flatten it before using array_unique
.
$unique = array_unique(array_merge(...$multi_array_example));
Or in older PHP versions (<5.6) before argument unpacking, you can use array_reduce
with array_merge
.
$unique = array_unique(array_reduce($multi_array_example, 'array_merge', []));
returns [1, 2, 3, 4, 5, 6]
Related Topics
How to Read the Query String in PHP and HTML
When Should I Use Closecursor() for Pdo Statements
PHP Fopen() Error: Failed to Open Stream: Permission Denied
How to Get Woocommerce Variation Id
How to Password Protect Streaming Videos with PHP
Alternative to MySQLi_Fetch_All Needed
Using PHP Replace Regex with Regex
Dump Facility in C++ Like Var_Dump() in PHP
Php.Ini Production VS Development
What Is the Use of Strict in Laravel Config/Database
In Flutter Web Getting 'Xmlhttprequest' Error While Making Http Call
Can't Install Xdebug on Windows
Output (Echo/Print) Everything from a PHP Array
Differencebetween .= and += in PHP
Understanding PHP "Out of Memory" Error
PHP Flush Stopped Flushing in Iis7.5
How to Get List of Supported Encodings by Iconv Library in PHP