So I have two arrays:
$one = array('red','green','blue','yellow','white');
$two = array('white','blue','red');
This being said, I need to now remove the elements from the first array that are existent in the second one. In short, the output after the sorting has to be (in this case): green, yellow.
I've looked at the array functions at PHP's documentation but was unable to find what I need. I'm sure it's something basic but I can't recall a function for that.
Try array_diff()
Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.
For example...
$three = array_diff($one, $two);
Demo ~ https://eval.in/167872
Related
I have two arrays with the same keys but different values. I need to merge it but if the values are the same leave only one of this
$array1 = array('firstname'=> $may_name, 'lastname'=>$my_last_name, 'address'=>$addres_1);
$array2 = array('firstname'=> $may_name, 'lastname'=>$my_last_name, 'address'=>$addres_2);
I need to get:
$array_result = array('firstname'=> $may_name, 'lastname'=>$my_last_name, 'address'=>$addres_1, 'address'=>$addres_2);
can anybody help to solve this?
array_merge does not work for me..
First you need to merge 2 arrays, using array_merge() function. then get the unique elements from the array using array_unique() function will get you the result
var_dump(array_unique(array_merge($array1, $array2)));
Edit
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
php doc
Thanks #Marco
I'm running into a problem where I'm trying to merge 2 (or more!) arrays in which the keys are not explicitly defined, and where I'm looking to not have any duplicates. This seems to be a silly problem to have, but I'm struggling to find an efficient way to solve it. Let me illustrate:
// for example, here are some arrays, the last 2 are the same...
$one = array(1);
$two = array(2);
$three = array(2);
Now, ideally I'd like to 'merge' these three arrays to have something like:
array(1, 2);
Using current PHP ability, this is what I have tried so far:
// this produces: array(1, 2, 2)
$merged = array_merge($one, $two, $three);
// this produces: array(2)
$merged = $one + $two + $three;
Both of which are not exactly ideal for what I need. Is there something obvious I'm missing? I really don't want to start looping through each array individually to check for duplicates before adding them to a merged array..!
Thanks in advance for any insights!
Just follow up array_merge with array_unique:
$merged = array_unique(array_merge($one, $two, $three));
Note that in the general case this will produce a result where the keys are not a sequence of integers. If that is a problem, reindex on top of that with array_values($merged).
If you are merging lots of items and you expect to have a substantial number of duplicates then it may be worth it to explore more efficient alternatives, but don't worry about it preemptively.
I'm trying to create a script that, based on an input a?? creates an array of all the combinations and permutations of all words containing an a and two other characters from the alphabet.
Values are such as a, ab, ba, dab, bga etc - as you may see the array contains (or should contain) a weird amount of values.
The problem is that the functions I use in the script outputs even more values with many duplicates.
And for some reason I can not create a flattened array without duplicates. I tried to use array_unique() but it doesn't work here. I tried to use explode() and implode() to flatten the result array, but no success. Even if I succeed to create a string from the values, when I try to transform this string into an array, the result is again the actual multi-dimensional array.
This drives me crazy, and as you see the code, I'm a beginner in PHP.
Any help to transform the actual multidimensional array to a flattened one without duplicates is highly appreciated. An example: actually the array contains 12168 sub-arrays, and only the string a occurs 1456 times. What I need is an array that doesn't have sub-arrays and contains each results only one time.
The PHP code is available at here
and the output is here:
Have you tried something like:
$inputString = 'a??';
$array = array();
if (strpos($inputString, 'a') !== false && !in_array($inputString, $array)) {
$array[] = $inputString;
}
echo '<pre>'; print_r($array); echo '</pre>';
This question already has answers here:
Check whether the array contain all element of other array?
(4 answers)
Closed 9 years ago.
I have two arrays, for example:
array1={1,2,3,4,5,6,7,8,9};
array2={4,6,9}
Is there any function so that I can determine that array2 fully exists in array1?
I know i can use the in_array() function in a loop but in cases where I will have large arrays with hundreds of elements so I am searching for a function.
Try:
$fullyExists = (count($array2) == count(array_intersect($array2, $array1));
The array_intersect.php function will return only elements of the second array that are present in all the other arguments (only the first array in this case). So, if the length of the intersection is equal to the lenght of the second array, the second array is fully contained by the first one.
You can use array_intersect for this, but you have to be a bit careful.
If the array to match has no duplicates, you can use
// The order of the arrays matters!
$isSubset = count(array_intersect($array2, $array1)) == count($array2);
However this will not work if e.g. $array2 = array(4, 4). If duplicates are an issue, you need to also use array_unique:
$unique = array_unique($array2);
// The order of the arrays matters!
$isSubset = count(array_intersect($unique, $array1)) == count($unique);
The reason that the order of the arrays matters is that the array given as the first parameter to array_intersect must have no duplicates. If the parameters are switched around this requirement will move from $array2 to $array1, which is important as it can change the behavior of the function.
Quick and easy solution:
array_diff(array(1,2,3,4,5,6,7,8,9),array(4,6,9));
if the return is a empty array, it is in the array otherwise he'll output the items that aren't
I didn't try with complex array but comparing work for me
var_dump(array(1,2,3,4,5,6,7,8,9) === array(4,6,9));
var_dump(array(1,2,3,4,5,6,7,8,9) === array(1,2,3,4,5,6,7,8,9));
I wanted to pull an arbitrary number of random elements from an array in php. I see that the array_rand() function pulls an arbitrary number of random keys from an array. All the examples I found online showed then using a key reference to get the actual values from the array, e.g.
$random_elements = array();
$random_keys = array_rand($source_array);
foreach ( $random_keys as $random_key ) {
$random_elements[] = $source_array[$random_key];
}
That seemed cumbersome to me; I was thinking I could do it more concisely. I would need either a function that plain-out returned random elements, instead of keys, or one that could convert keys to elements, so I could do something like this:
$random_elements = keys_to_elements(array_rand($source_array, $number, $source_array));
But I didn't find any such function(s) in the manual nor in googling. Am I overlooking the obvious?
What about usung array_flip? Just came to my mind:
$random_elements = array_rand(array_flip($source_array), 3);
First we flip the array making its values become keys, and then use array_rand.
An alternate solution would be to shuffle the array and return a slice from the start of it.
Or, if you don't want to alter the array, you could do:
array_intersect_key($source_array, array_combine(
array_rand($source_array, $number), range(1, $number)));
This is a bit hacky because array_intersect can work on keys or values, but not selecting keys from one array that match values in another. So, I need to use array_combine to turn those values into keys of another array.
You could do something like this, not tested!!!
array_walk(array_rand($array, 2), create_function('&$value,$key',
'$value = '.$array[$value].';'));