array-unique

multidimensional array conversion is not working

感情迁移 提交于 2019-12-08 12:00:48
问题 Very frustrating moment and could not figure out and finally decided to move and ask to Geeks about solution. Okay, Here is the problem. I have below array and need to convert it by replacing repeating records from array and merge is as shown below. Array ( [0] => Array ( [Index] => Array ( [id] => 2 [content] => content 2 [user_id] => 1 [page_number] => 25 [book_id] => 1 [tag_id] => 3,2,4 [need_review] => no [status] => active [created] => 2013-06-27 12:36:45 [modified] => 2013-06-27 12:36

Array_unique on a laravel eloquent collection

北慕城南 提交于 2019-12-06 18:20:17
问题 Not sure if this is possible but im trying to run array_unique over a collection of items i have, to remove duplicates. Although i cannot get it working. my controller logic: // init models $jobs = Job::search(); $countries = $jobs->get()->map(function( $job ) { return $job->country; }); $countries = array_unique( $countries->toArray() ); although this gets a "Array to string conversion" error 回答1: You can have unique values in your DB results using distinct or group by in your select clause.

Why does array_unique sort the values?

独自空忆成欢 提交于 2019-12-05 02:33:30
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! If you think about it algorithmically, the way to remove duplicates is to go through a list, keep track of items you find, and get rid of things that are already in that "found this" list. One easy way to accomplish

Array_unique on a laravel eloquent collection

删除回忆录丶 提交于 2019-12-05 01:47:06
Not sure if this is possible but im trying to run array_unique over a collection of items i have, to remove duplicates. Although i cannot get it working. my controller logic: // init models $jobs = Job::search(); $countries = $jobs->get()->map(function( $job ) { return $job->country; }); $countries = array_unique( $countries->toArray() ); although this gets a "Array to string conversion" error You can have unique values in your DB results using distinct or group by in your select clause. But, if you really need to have unique values over an array of object you can do the following: $uniques =

array_unique for arrays inside array

浪尽此生 提交于 2019-12-01 13:51:10
问题 I need a function like array_unique for arrays inside array. The Case - should be equal, but output "not equal": <?php $arr=array(array('a',1),array('a',2)); $arr2=array_unique($arr); if($arr2==$arr){ echo "equal"; } else{ echo "not equal"; } ?> How should the code be changed to get output "equal"? 回答1: You should modify your call for array_unique to have it include the SORT_REGULAR flag. $arr2 = array_unique($arr, SORT_REGULAR); 回答2: If you want to test if the outer array has unique entries,

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

試著忘記壹切 提交于 2019-12-01 05:53:11
This question already has an answer here: case-insensitive array_unique 5 answers 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 duplicate green ? As far as which one to remove, we assume that duplicates with uppercase characters are correct. e.g. keep PHP

array_merge & array_unique

爱⌒轻易说出口 提交于 2019-11-29 10:14:01
Is there an array function in PHP that somehow does array_merge, comparing the values , ignoring the keys? I think that array_unique(array_merge($a, $b)) works, however I believe there must be a nicer way to do this. eg. $a = array(0 => 0, 1 => 1, 2 => 2); $b = array(0 => 2, 1 => 3, 2 => 4); resulting in: $ab = array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4); Please note that I don't care about the keys in $ab , however it would be nice if they were ascending, starting at 0 to count($ab)-1 . The most elegant, simple, and efficient solution is the one mentioned in the original question... $ab =

php filter array values and remove duplicates from multi dimensional array

六月ゝ 毕业季﹏ 提交于 2019-11-28 10:39:40
问题 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 [y] => 21.75 ) [2] => Array ( [x] => 14.25 [y] => 21.875 ) [3] => Array ( [x] => 19.375 [y] => 21.75 ) [4] => Array ( [x] => 9.125 [y] => 21.875 ) [5] => Array ( [x] => 23 [y] => 19.625 ) [6] => Array ( [x] => 19.375 [y] => 19.625 ) ) So what i need to happen is loops through the entire

Select only unique array values from this array

六眼飞鱼酱① 提交于 2019-11-28 09:11:48
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 : Object of class stdClass could not be converted to string on line 191 $uniques = array(); foreach (

array_merge & array_unique

霸气de小男生 提交于 2019-11-28 03:29:23
问题 Is there an array function in PHP that somehow does array_merge, comparing the values , ignoring the keys? I think that array_unique(array_merge($a, $b)) works, however I believe there must be a nicer way to do this. eg. $a = array(0 => 0, 1 => 1, 2 => 2); $b = array(0 => 2, 1 => 3, 2 => 4); resulting in: $ab = array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4); Please note that I don't care about the keys in $ab , however it would be nice if they were ascending, starting at 0 to count($ab)-1 .