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
1 answer

array_unique() in php simple html dom

I wrote the code blow to get all unique links from a url: include_once ('simple_html_dom.php'); $html = file_get_html('http://www.example.com'); foreach($html->find('a') as $element){ $input = array($element->href = $element->href . '
'); …
user6519181
1
vote
2 answers

How to Remove duplicate values from Multidimentional array with single unique value in all array

I have multi-dimentional array like below, $product = array( "2e471a22b1b994a7cb3f3a40cee9fba2" => array ( "product" => 6004, "unique_key" => 3a8a5cb029ee3b92cfc90de23e2329ab, …
Ramkumar
  • 23
  • 7
1
vote
0 answers

Session Array - array_unique & array_slice to limit returned values

I'm trying to return the values of a session array (productsviewed) which I have working using the following code: $val=$ProdGrp; array_push($_SESSION[productsviewed],$val); //print_R($_SESSION[productsviewed]); foreach…
1
vote
3 answers

array_unique in php with subarray in array

How can i use array_unique function for this array ` $mon = array('9:00AM - 11:00AM','1:00pm-6pm'); $tue = array('8:00AM - 11:00AM','12:00pm-6pm'); $wed = array('9:00AM - 11:00AM','1:00pm-6pm'); $thu = array('9:00AM -…
Ashutosh Jha
  • 186
  • 1
  • 3
  • 11
1
vote
3 answers

How to merge an array with duplicate keys with array merge?

$cat1=array( "_id"=>new MongoId("562918fc2bad8c345d000029"),"name"=>"Category Two"); $cat2=array("_id"=>new MongoId("562918e62bad8c445d000029"), "name"=>"Category…
phpnerd
  • 850
  • 1
  • 10
  • 25
1
vote
1 answer

array_unique() not functioning as it should?

I have an array in variable $votes. print_r($votes) gives us: Array ( [0] => 1 [1] => 1 [2] => 1 ) So we have three values, all of which are set to 1. Now, I want to make the array only have unique values, meaning that if there are three values…
Henrik Petterson
  • 6,862
  • 20
  • 71
  • 155
1
vote
2 answers

PHP arrays: delete duplicates and reorder keys

I have this array: $gspa['aryNumEmp'] : array = 0: string = 41 1: string = 41 2: string = 41 3: string = 41 4: string = 41 5: string = 41 6: string = 41 7: string = 41 8: string = 41 9: string = 2355 10: string = 2355 11:…
ADM
  • 1,590
  • 11
  • 36
  • 54
1
vote
3 answers

Make a unique list of values from a particular key existing anywhere in a deep array

I have an array that consists of an undetermined number of arrays, recursively (n levels deep). Each array might contain a name key. I want to create a unique list of those values. Example Suppose the array is: $bigArray = array( 'name'=>'one',…
1
vote
2 answers

array_unique does not sort a simple string array

According to the PHP documentation, array_unique removes duplicate values and sorts the values treated as string at first. When you take the following snippet in mind: // define a simple array of strings, unordered $brands = [ "BMW", "Audi", "BMW",…
Juliën
  • 9,047
  • 7
  • 49
  • 80
1
vote
1 answer

array_unique not sorting values from regular expression

So I have the following regex and I count the occurrences of matches like so: preg_match_all("/".$toArray."/i", $input, $matches); $count_matches = count($matches[0]); However, I want to get only those matches that are unique. And applying…
1
vote
3 answers

Get array element with sub elements without repeating in PHP

I walk around here with some hesitation, I have passed an array with sub elements (so to speak) and I need three random values ​​but these are obtained without repeating. The array is as follows: Array ( [0] => Array …
Steeep
  • 11
  • 1
1
vote
3 answers

Remove duplicates from multidimensional associative array in php if two values match

I have a multidimensional array of the following structure and I want to remove duplicates from it. For example if the ["amount"] is the same for two ["cities"] but the ["time"] is the same or different then I count this is a duplicate and want to…
Ben Paton
  • 1,432
  • 9
  • 35
  • 59
1
vote
6 answers

How to remove the parent numeric keys of php array

I've an array in php something like below Array ( [0] => Array ( [0] => 40173 [1] => 514081 [2] => 363885 [3] => 891382 ), [1] => Array ( [0] => 40173 …
Manish Jangir
  • 5,329
  • 4
  • 42
  • 75
1
vote
1 answer

Does array_unique() modify its array by reference?

After I use array_unique() on an array, when I var_dump() the array, it still has the same content, with duplicates: array(21) { [0]=> string(10) "tricou_CRS" [1]=> string(10) "tricou_CRM" [2]=> string(11) "tricou_CRXL" [3]=> string(10)…
Mihai Bujanca
  • 4,089
  • 10
  • 43
  • 84
1
vote
2 answers

Creating a unique array on some conditon PHP

I have an associative array $result represented as $result[0]['id']=120 $result[0]['point']=3.4 $result[1]['id']=136 $result[1]['point']=4.5 $result[2]['id']=140 $result[2]['point']=5.6 $result[3]['id']=120 $result[3]['point']=6.7 I want to make…
user3357227
  • 139
  • 1
  • 2
  • 10