Questions tagged [array-unique]

PHP's array_unique() function accepts a single array and returns a new array with all duplicate values removed

PHP's array_unique() function accepts a single array and returns a new array with all duplicate values removed. The second optional parameter is used to modify the ordering of the values in the returned array

Example:

$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);

Result:

Array
(
    [a] => green
    [0] => red
    [1] => blue
)
180 questions
75
votes
1 answer

array_unique and then renumbering keys

Possible Duplicate: Re-index numeric array keys I have an array as follows Array ( [0] => 15/11/2012 - 18/11/2012 [1] => 15/11/2012 - 18/11/2012 [2] => 15/11/2012 - 18/11/2012 [3] => 15/11/2012 - 18/11/2012 [4] => 19/12/2012 -…
Franco
  • 2,846
  • 7
  • 35
  • 54
32
votes
4 answers

array_unique vs array_flip

If I had an array of signed integers e.g: Array ( [0] => -3 [1] => 1 [2] => 2 [3] => 3 [4] => 3 ) To get unique values I would instinctively use array_unique but after consideration I could perform array_flip twice which would…
Lizard
  • 43,732
  • 39
  • 106
  • 167
25
votes
9 answers

Remove duplicate items from an array

I use the line of code below to loop through a table in my database: $items_thread = $connection -> fetch_all($sql); And if I print the array out: print_r($items_thread); I will get this: Array ( [0] => Array ( …
Run
  • 54,938
  • 169
  • 450
  • 748
24
votes
7 answers

How do I use array_unique on an array of arrays?

I have an array Array( [0] => Array ( [0] => 33 [user_id] => 33 [1] => 3 [frame_id] => 3 ) [1] => Array ( [0] => 33 [user_id] => 33 [1] => 3 [frame_id] => 3 ) [2] =>…
dotty
  • 40,405
  • 66
  • 150
  • 195
22
votes
3 answers

array_unique for arrays inside array

I need a function like array_unique for arrays inside array. The Case - should be equal, but output "not equal":
Ben
  • 25,389
  • 34
  • 109
  • 165
18
votes
4 answers

Array_unique on a laravel eloquent collection

Not sure if this is possible but im trying to run array_unique over a collection of items i have, to remove duplicates. Although i cannot get it working. my controller logic: // init models $jobs = Job::search(); $countries =…
AndrewMcLagan
  • 13,459
  • 23
  • 91
  • 158
14
votes
5 answers

array_merge & array_unique

Is there an array function in PHP that somehow does array_merge, comparing the values, ignoring the keys? I think that array_unique(array_merge($a, $b)) works, however I believe there must be a nicer way to do this. eg. $a = array(0 => 0, 1 => 1, 2…
ptmr.io
  • 2,115
  • 4
  • 22
  • 34
13
votes
3 answers

What is better to use: in_array or array_unique?

I am in doubt what to use: foreach(){ // ..... if(!in_array($view, $this->_views[$condition])) array_push($this->_views[$condition], $view); // .... } OR foreach(){ // ..... array_push($this->_views[$condition],…
user1692333
  • 2,461
  • 5
  • 32
  • 64
13
votes
4 answers

Select only unique array values from this array

I have the following variable $rows: Array ( [0] => stdClass Object ( [product_sku] => PCH20 ) [1] => stdClass Object ( [product_sku] => PCH20 ) [2] => stdClass Object ( [product_sku] => PCH19 …
RhymeGuy
  • 2,102
  • 5
  • 32
  • 62
9
votes
2 answers

Why does array_unique sort the values?

This refers to one of my previous questions: array_unique vs array_flip - This states that array_flip(array_flip()) is much quicker than array_unique() when dealing with simple strings and integers. What I would like to know is why array_unique()…
Lizard
  • 43,732
  • 39
  • 106
  • 167
7
votes
3 answers

Ruby removing duplicates from array based on key=>value

I have an array of Musical Tracks and in this array the same song can show up multiple times due to being released on multiple albums. I am trying to remove them from the array so that only true uniques show up in the list. The Hash looks something…
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
7
votes
3 answers

php array_unique sorting behavior

I am checking the array_unique function. The manual says that it will also sort the values. But I cannot see that it is sorting the values. Please see my sample code. $input = array("a" => "green", 3=>"red", "b" => "green", 1=>"blue",…
Pramod Sivadas
  • 873
  • 2
  • 9
  • 28
6
votes
1 answer

PHP Remove Duplicates from an array

I am having a bit of an issue with my PHP array. First thing they have arrays inside of this array and I am trying to remove duplicates. I did a print_r of my array and it printed out this.... Array ( [0] => Array ( [creditfeeid] => 318 [client]…
user979331
  • 11,039
  • 73
  • 223
  • 418
6
votes
4 answers

PHP Array unique values

I have an array like this Array ( [0] => Array ( [id] => BA [name] => British Airways ) [1] => Array ( [id] => BA [name] => British Airways ) [2] =>…
Itsmeromka
  • 3,621
  • 9
  • 46
  • 79
5
votes
5 answers

Best solution to remove duplicate values from case-insensitive array

I found a few solutions but I can't decide which one to use. What is the most compact and effective solution to use php's array_unique() function on a case-insensitive array? Example: $input = array('green', 'Green', 'blue', 'yellow',…
CyberJunkie
  • 21,596
  • 59
  • 148
  • 215
1
2 3
11 12