array-unique

Why does array_unique sort the values?

爱⌒轻易说出口 提交于 2020-01-23 05:23:08
问题 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() creates a copy of the array, sorts it then removed the duplicates The source for both functions is available here. Thanks in advance! 回答1: If you think about it algorithmically, the way to remove duplicates is to go through a list, keep track of

Best solution to remove duplicate values from case-insensitive array [duplicate]

六眼飞鱼酱① 提交于 2020-01-21 07:24:13
问题 This question already has answers here : case-insensitive array_unique (5 answers) Closed 5 months ago . 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', 'blue'); $result = array_unique($input); print_r($result); Result: Array ( [0] => green [1] => Green [2] => blue [3] => yellow ) How do we remove the

Select only unique array values from this array

若如初见. 提交于 2020-01-09 10:07:30
问题 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 ) [3] => stdClass Object ( [product_sku] => PCH19 ) ) I need to create second array $second containing only unique values: Array ( [0] => stdClass Object ( [product_sku] => PCH20 ) [1] => stdClass Object ( [product_sku] => PCH19 ) ) But when i run array_unique on $rows, i receive: Catchable fatal error

Select only unique array values from this array

霸气de小男生 提交于 2020-01-09 10:02:49
问题 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 ) [3] => stdClass Object ( [product_sku] => PCH19 ) ) I need to create second array $second containing only unique values: Array ( [0] => stdClass Object ( [product_sku] => PCH20 ) [1] => stdClass Object ( [product_sku] => PCH19 ) ) But when i run array_unique on $rows, i receive: Catchable fatal error

Why doesn't this array_unique work as expected?

♀尐吖头ヾ 提交于 2019-12-23 12:23:40
问题 Can anybody tell me why this doesn't work as expected? <?php $merchant_string = '123-Reg|Woolovers|Roxio|Roxio|BandQ|Roxio|Roxio|Big Bathroom Shop|Roxio|Robert Dyas|Roxio|Roxio|PriceMinister UK|Cheap Suites|Kaspersky|Argos|Argos|SuperFit|PriceMinister UK|Roxio|123-Reg'; $merchant_array = explode('|', $merchant_string); for($i = 0; $i<count($merchant_array); $i++) { $merchant_array = array_unique($merchant_array); echo $merchant_array[$i] . '<br />'; } ?> The results I get is: Woolovers Roxio

array_unique vs array_flip

自闭症网瘾萝莉.ら 提交于 2019-12-17 15:36:07
问题 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 have the same effect, and I think it would be quicker? array_unique O(n log n) because of the sort operation it uses array_flip O(n) Am I correct in my assumptions? UPDATE / EXAMPLE: $intArray1 = array(-4,1,2,3); print_r($intArray1); $intArray1 = array_flip($intArray1);

possible limitation of implode function in PHP

匆匆过客 提交于 2019-12-13 15:40:44
问题 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 nothing var_dump($organizers); // no data appears here exit; The array_unique() function is returning data correctly and I can see the array it returns. To start, the $organizers array is a simple 1-D array of strings that all have small lengths under 20 chars

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

核能气质少年 提交于 2019-12-13 00:22:42
问题 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', 'something'=>array( 'name'=>'two', 'subthing'=>array('name'=>'three') ), 'anotherthing'=>array('name'=>'one') ); The expected result would be: $uniques = array('one', 'two', 'three') // All the 'name' keys values and without duplicates. Here's a fiddle

PHP - Using implode, explode, and array_unique to autopopulate a dropdown menu from two SQL columns of comma-separated keywords

我与影子孤独终老i 提交于 2019-12-12 02:18:44
问题 I have an SQL database with a "category" keyword (only one allowed) and "issues" keywords (multiple comma-separated words). I am trying to make a auto-populating drop-down keyword select menu by selecting all the keywords from the "category" and "issues" columns, turning both returned arrays into comma-separated strings with implode, then combining the strings and exploding the comma-separated strings into an array, while removing duplicate entries with array_unique. But it's not working. I

PHP print out only unique values from array

喜夏-厌秋 提交于 2019-12-11 19:56:28
问题 I'm hoping someone can help me with a problem I am having returning only unique values from an array. I am pulling data from the 500px API of photographs that I have favourited. From this array I would like to pull out the category IDs of the photos. The below does the job to a certain extent, but I want to show only unique values (ultimately I want to build a navigation from the labels associated with the IDs). if($json){ $obj = json_decode($json); } else { print "<p>Currently, No Service