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
1
vote
3 answers

PHP Array_unique

Is it possible to remove sub arrays, if the array item at position 0 of that sub array matches subsequent items? For example; Array ( [0] => Array ( [0] => 1234 [1] => XX000 ) [1] => Array ( [0] => 1234 [1] => XX001 ) [2] => Array…
user1235285
  • 87
  • 2
  • 17
1
vote
3 answers

Why is my filter method not removing some elements that should be removed?

I am trying to create a method called filer_out! that takes in an array and a proc, and returns the same array but with every element that returns true when it is run through the proc, with the caveat being we can't use Array#reject! I wrote…
Chad Cunningham
  • 165
  • 1
  • 1
  • 7
1
vote
2 answers

Find duplicates in a multidimensional array and echo out them

I want to find duplicates in a multidimensional array and then echo out which one who has duplicates and which not. I've tried this code. $teams = array ( "grupp_A" => array('Arsenal','Arsenal','Barcelona'), "grupp_B" => array('Milan','Man…
Anders
  • 63
  • 4
1
vote
1 answer

Invalid JSON format in using array_unique function

I have removed the duplicate items for the following array, by writing: $outcome['id'] = array_unique($outcome['id'], SORT_REGULAR); but I'm getting undesirable JSON output "id": { "0": { "id": 947, "label":…
1
vote
0 answers

What change does array_unique() make when migrating to PHP 7.2?

Here is the description of array_unique() in PHP 7.2: If sort_flags is SORT_STRING, formerly array has been copied and non-unique elements have been removed (without packing the array afterwards), but now a new array is built by adding the…
TungPS
  • 51
  • 6
1
vote
2 answers

PHP - Array unique only using specific values

I have a PHP array that looks like this... Array ( [0] => Array ( [id] => 1 [value] => 111 [date] => 'today' ) [1] => Array ( [id] => 2 [value] => 222 [date] => 'today' ) [2] =>…
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
1
vote
3 answers

Duplicate remove from foreach

I want to remove duplicates image. How to use array_unique? This is what I have tried: $text = 'text image.jpg flower.jpg txt image.jpg'; $pattern = '/[\w\-]+\.(jpg|png|gif|jpeg)/'; $result = preg_match_all($pattern, $text, $matches); $matches =…
harry
  • 33
  • 5
1
vote
1 answer

Select unique arrays from multidimensional array with exception of date

I have a multidimensional array like this Array ( [0] => Array ( [name] => test [c1] => flower [c2] => fruit [date] => 2017-10-05 10:44:05 ) [1] => Array ( …
Wali Hassan
  • 480
  • 5
  • 15
1
vote
3 answers

PHP - Delete all duplicates in array

How can I delete duplicates from multiple arrays? pinky and cocos are double in my array. All words which are double, must be removed. If those are removed, I will put these words in my select. I get those words from my database. The…
user8631931
1
vote
1 answer

array_unique outputs null while sorting array

I have this array (decoded from JSON, output with print_r): stdClass Object ( [data] => Array ( [0] => stdClass Object ( [item] => te [date] => 13.10 ) …
Francis
  • 343
  • 2
  • 5
  • 16
1
vote
2 answers

Array Unique for Array of Objects with exclude option for certain keys in Object

I have an Object Array with few fields. I was able to get the Array unique for this Object Array using below code: $aa = array_unique($dd,SORT_REGULAR); But what i am trying to achieve is, i want to exclude certain keys in object from "unique…
Wolverine
  • 455
  • 3
  • 8
  • 26
1
vote
4 answers

How to merge subarray values and generate a 1-dimensional array of unique values?

How to get final unique array result from multiple array? I have an array like this: Array ( [0] => Array ( [0] => 8 [1] => 9 [2] => 7 ) [1] => Array ( [0] => 7 …
1
vote
2 answers

how to remove duplicates php array_unique not working

I try using array_unique(); but not working this is my code please help me what i doing wrong? I want display all tags without duplicates ID | TAGS 1 | rock, punk, jazz 2 | pop, rock, classic 3 | jazz, blues, rock 4 | rock, rap, metal $wynik =…
Tomasz
  • 27
  • 8
1
vote
1 answer

How to convert object format to json format in php

I'm using array_unique to remove the duplicate values but it returns an object format. $event = array_unique($array_event); output as {"0":{"title":"new","description":"hai"},"4":null} Excepted…
Raghul Rajendran
  • 486
  • 2
  • 7
  • 25
1
vote
3 answers

How to remove `null` result when using array_unique?

I have the following multidimensional array: $subregion = [ [ "subregion_id" => "8", "subregion_name" => "NORTH_AMERICA", "subregion_abbr" => "US" ], [ "subregion_id" => "9", "subregion_name"…
ReynierPM
  • 17,594
  • 53
  • 193
  • 363