array-intersect

How can I get case-sensitive return from array_intersect()

时间秒杀一切 提交于 2019-11-30 23:04:41
I have two arrays and I need to compare that and return matched value from array1. Please refer my code below, $array1 = array("a" => "Green", "Red", "Blue"); $array2 = array("b" => "grEEn", "yellow", "red"); $result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2)); print_r($result); My result is, Array ( [a] => green [0] => red ) But my expected result is I want get it from array1 like: Array ( [a] => Green [0] => Red ) This is because you put all values to lowercase. Just change to array_uintersect() and use strcasecmp() as callback function to compare

How can I get case-sensitive return from array_intersect()

烈酒焚心 提交于 2019-11-30 17:20:13
问题 I have two arrays and I need to compare that and return matched value from array1. Please refer my code below, $array1 = array("a" => "Green", "Red", "Blue"); $array2 = array("b" => "grEEn", "yellow", "red"); $result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2)); print_r($result); My result is, Array ( [a] => green [0] => red ) But my expected result is I want get it from array1 like: Array ( [a] => Green [0] => Red ) 回答1: This is because you put all

PHP: How to compare keys in one array with values in another, and return matches?

对着背影说爱祢 提交于 2019-11-30 12:50:13
I have the following two arrays: $array_one = array('colorZero'=>'black', 'colorOne'=>'red', 'colorTwo'=>'green', 'colorThree'=>'blue', 'colorFour'=>'purple', 'colorFive'=>'golden'); $array_two = array('colorOne', 'colorTwo', 'colorThree'); I want an array from $array_one which only contains the key-value pairs whose keys are members of $array_two (either by making a new array or removing the rest of the elements from $array_one ) How can I do that? I looked into array_diff and array_intersect , but they compare values with values, and not the values of one array with the keys of the other. If

Checking if an array contains all elements of another array

不问归期 提交于 2019-11-28 13:38:48
I'm designing an electrical engineering application. However, i'm stuck on this: I have the following array <?php // Static Array $GroupOfEight = array ( array(0,1,3,2,4,5,7,6), array(4,5,6,7,16,12,13,14), array(12,13,15,14,8,9,11,10), array(2,6,14,10,3,7,15,11), array(1,3,5,7,13,15,9,11), array(0,4,12,8,1,5,13,9), array(0,1,3,2,8,9,11,10) ); ?> And I have another array, but this one is one dimensional. <?php $myStack = array(0,1,3,2,4,5,7,6); //Dynamic, gets value by POST method. ?> What I want to do is to check if $myStack is equal to any sub array of $GroupOfEight array. ( Number ordering

Check if an array contains another array with PHP [duplicate]

喜你入骨 提交于 2019-11-28 07:45:37
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Checking if an array contains all elements of another array I have posted something like this to the Stackoverflow before, but the answers do not fully satisfy me. That's why I'm posting the question again, but changing the question all along. Some people helped me to construct a function that checks if an array($GroupOfEight[$i]) that is an element of a multidimensional array($GroupOfEight) equals another array

Python intersection of two lists keeping duplicates

拜拜、爱过 提交于 2019-11-27 16:07:05
I have two flat lists where one of them contains duplicate values. For example, array1 = [1,4,4,7,10,10,10,15,16,17,18,20] array2 = [4,6,7,8,9,10] I need to find values in array1 that are also in array2, KEEPING THE DUPLICATES in array1. Desired outcome will be result = [4,4,7,10,10,10] I want to avoid loops as actual arrays will contain over millions of values. I have tried various set and intersect combinations, but just couldn't keep the duplicates.. Any help will be greatly appreciated! What do you mean you don't want to use loops? You're going to have to iterate over it one way or another

Using array_intersect on a multi-dimensional array

馋奶兔 提交于 2019-11-26 16:13:55
问题 I have two arrays that both look like this: Array ( [0] => Array ( [name] => STRING [value] => STRING ) [1] => Array ( [name] => STRING [value] => STRING ) [2] => Array ( [name] => STRING [value] => STRING ) ) and I would like to be able to replicate array_intersect by comparing the ID of the sub arrays within the two master arrays. So far, I haven't been successful in my attempts. :( 回答1: Use array_uintersect() to use a custom comparison function, like this: $arr1 = array( array('name' =>