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

How to get the name of a key in array

This sounds quite simple, but i can't make it work. I'm trying to group keys with same value. I can get the key number but i cant get the name of the Key. i.e "London, Berlin". This is my code: $countries = array ( 'London' => 'Europe/London', …
Avel
  • 111
  • 1
  • 2
  • 9
2
votes
4 answers

remove duplicates and move those that had duplicates to beginning of multi-dimensional array php

I'm creating a search function for a csv. With the result of my search I then want to remove duplicates and then move those that had duplicates to the beginning of the array. Array ( [0] => Array ( [0] => COD 21 …
2
votes
3 answers

Get unique array for $key

I have the following code: $keys = array('2', '2', '6', '10', '10', '10', '13', '13', '13', '13', '13', '13', '13', '13', '13', '13', '15', '16', '18', '18', '18', '18', ); $values = array('1712', '1712', '1977', '231', '245', '245', '11', '11',…
Bernedette
  • 15
  • 3
2
votes
1 answer

php array_unique() returns duplicate

I have some code running and it's been working fine BUT the site in question has started producing a duplicate when the value in an array is "morphsuite" The code: if(isset($sort2)) { $sort2 = array_unique($sort2); foreach($sort2 as…
john
  • 349
  • 8
  • 15
2
votes
3 answers

possible limitation of implode function in PHP

I have the following code that is not returning as I expected. I was hoping the final result would be a string: $organizers = array_unique($organizers); // this returns correctly $organizers = implode(', ', $organizers); // this returns…
usumoio
  • 3,500
  • 6
  • 31
  • 57
2
votes
4 answers

array_unique only returns one record

In the code below: $sth = $dbh->query('SELECT DISTINCT title,courseId,location from training'); $sth->setFetchMode(PDO::FETCH_ASSOC); $results = $sth->fetchAll(); $uu = array_unique($results); echo "
";
print_r($uu);
echo "
"; I only…
Paul Dessert
  • 6,363
  • 8
  • 47
  • 74
2
votes
3 answers

array_unique not working as expected in php

Here is a problem. i am exploding a list of character by new line in an array. and doing array unique on it. but it is not working as expected. below is code: $list = "test ok test test ok ok test"; $list_explode = explode("\n", $list); //exploding…
Yalamber
  • 7,360
  • 15
  • 63
  • 89
1
vote
3 answers

array_unique not working in PHP?

I have got a table in my MySql database: pageid | text -----------+---------- 1 | test -----------+---------- 2 | example -----------+---------- 3 | another -----------+---------- 1 | example1 and this PHP…
Akos
  • 1,997
  • 6
  • 27
  • 40
1
vote
1 answer

How do I adapt/utilize array_unique() to handle objects?

I have objects such as these: class Log { public matches; public function __construct() { $this->matches = array(); } } class Match { public owner; public stuff; } Throughout my program, I store data into $owner…
user890443
1
vote
2 answers

Error with array_unique

What am I doing wrong with this? If I just run this: $region = EM_Locations::get(array('orderby'=>'region_name')); all is good. However when I add in the array_unique: $region = EM_Locations::get(array('orderby'=>'region_name')); $reg =…
RonnieT
  • 2,193
  • 4
  • 32
  • 43
1
vote
0 answers

How do I get "explode" and "array_unique" merged

i have two tables, one separates tags by comma, the other converts similar tags to single tags. example: The exploded tags table I got in the database:
1
vote
3 answers

php array_unique with exceptions

I want to remove duplicate values in an array except 1 value. Eg: $array = array ("apple", "orange", "orange", "banana", "grapes","grapes", "apple"); How can I remove all duplicate values and keep all duplicate values that equal "apple" $array =…
vxd
  • 11
  • 1
1
vote
2 answers

Delete Duplicate from Two arrays and display one of the arrays in javascript

I want delete duplicate array from two arrays, but just show one of array, how I can do it ? I want result [1, 4] const arr1 = [1, 2, 3, 4]; const arr2 = [2, 3, 5, 6] function arrayUniq(arr1, arr2) { enter code here }
mazbeib
  • 11
  • 2
1
vote
0 answers

Merging Multi-dimensional Array (Custom) PHP - Laravel

Merge a multi-dimensional array to the custom array as required. Need the solution in PHP (using in Laravel-8). Custom array is needed to make rows for creating an excel file using Spreadsheet. This is the Original array I have = array:3 [ 0 =>…
1
vote
2 answers

How to use array_unique correctly to echo only one time the same posts

I am trying to display the posts of some RSS Feeds and I came up with a question/problem I have, when I have two same feeds I am trying to show not all the posts but the unique. What I was using is this, that shows me all the posts twice (this is…
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
1 2
3
11 12