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
5
votes
2 answers

array_unique with SORT_NUMBERIC behaviour

I've stumbled upon something weird and I don't understand why it works that way. I have an array of numbers, they are all unique: $array = [ 98602142989816970, 98602142989816971, 98602142989816980, 98602142989816981, …
Arthur Shveida
  • 447
  • 2
  • 8
5
votes
2 answers

How to remove duplicate words from string using php?

Below is the variable I have, $string = 'AAA,BBB,aAA,BbB,AAA,BbB'; I need the unique string result below, $string = 'AAA,BBB,aAA,BbB'; How to make it unique just like array_unique() function , is there any default String function to remove…
Thiyagu
  • 746
  • 3
  • 11
  • 29
5
votes
2 answers

php array_unique not working as expected

I am trying to learn how to use array_unique, so I made some sample code and I didn't get what I expected. $array[0] = 1; $array[1] = 5; $array[2] = 2; $array[3] = 6; $array[4] = 3; $array[5] = 3; $array[6] = 7; $uniques = array_unique($array,…
mucle6
  • 645
  • 1
  • 10
  • 24
4
votes
1 answer

Why doesn't this array_unique work as expected?

Can anybody tell me why this doesn't work as expected?
Keith Donegan
  • 26,213
  • 34
  • 94
  • 129
4
votes
4 answers

Removing duplicates with array_unique

I created a image uploader which works by remote, so whenever a user enters a bunch of links, I want to prevent duplicate links being added so that the image isn't copied twice and is removed so it leaves the links to be unique without any…
MacMac
  • 34,294
  • 55
  • 151
  • 222
4
votes
1 answer

SELECT DISTINCT or array_unique?

What do you think is faster? SELECT DISTINCT(user_id) FROM myTable; Then fetch to $myArray. Or SELECT user_id FROM myTable; Then fetch to $myArray and do $myArray = array_unique($myArray); Note: user_id is a FOREIGN KEY CONSTRAINT
Nicholaos Renessis
  • 432
  • 1
  • 4
  • 8
3
votes
1 answer

Number of actions of array_unique PHP

Does anyone know the Big O of array_unique()? I haven't gone through the source, but I would imagine it loops through each value and checks to to see if it is in the array which would be O(n^2) is this correct? Thanks
Lizard
  • 43,732
  • 39
  • 106
  • 167
3
votes
5 answers

How to remove duplicate values from array in foreach loop?

I want to remove duplicate values from array. I know to use array_unique(array) function but faced problem in foreach loop. This is not a duplicate question because I have read several questions regarding this and most of them force to use…
Bandara
  • 283
  • 2
  • 4
  • 17
3
votes
2 answers

How can you get unique values from multiple arrays using array_unique?

Is there any way to put two or more arrays into the array_unique() function? If not, is there any other solution to get unique results from multiple arrays?
user6519181
3
votes
5 answers

php filter array values and remove duplicates from multi dimensional array

Hello all im trying to find duplicate x values from this array and remove them and only leave the unique ones. For example my array is Array ( [0] => Array ( [x] => 0.5 [y] => 23 ) [1] => Array ( [x] => 23 …
Yeak
  • 2,470
  • 9
  • 45
  • 71
3
votes
2 answers

Split two delimited strings into arrays of words, merge, then remove duplicates

I'm struggling to remove duplicates words found within two strings. When I call array_unique, the duplicated words are not removed. $a = 'mysql string 1';// basically words A, B, C $b = 'mysql string 2';// basically words D, A, E $a_b_array =…
Alforreca
  • 35
  • 4
2
votes
1 answer

array_unique 5-dimensional array php

I am looking for an elegant way to dedupe a 5-dimensional array like thaht : Array ( [LOADERS] => Array ( [S130] => Array ( [527311001 & Above] => Array ( …
Raphaël
  • 1,141
  • 3
  • 18
  • 32
2
votes
3 answers

What alternative of code that work in PHP7 in PHP5

I have a PHP code which runs on PHP7 but not in PHP 5, which PHP version in my server, that is my code: Array ( [0] => stdClass Object ( [userId] => 15 [name] => name0 [userName] => hh [centerName] => center10 …
Ali A. Jalil
  • 873
  • 11
  • 25
2
votes
1 answer

Extract description from EXIF data and exclude duplicate text

I'm working on an image gallery with portraits where I extract text from Exif data. When I edit the images I am writing the names of the people which I later show in my image gallery as captions. I would like to make a list, but can't figure out how…
kdt2017
  • 23
  • 3
2
votes
1 answer

Remove duplicate values from multidimensional (3+levels) array with PHP

I need your help. I've done various research and no testing method worked with 3 levels or more arrays. I need to leave only the only ones values in the multidimensional array .. My Array: Array ( [sucesso] => Array ( …
1
2
3
11 12