How to return an array with true / false values ​​by comparing 2 arrays?

I am trying to create an array as a result of comparing 2 arrays: the 1st array has more elements, such as:

array1: { 0 => "car1" 1 => "car2" 2 => "car3" 3 => "car4" 4 => "car5" } 

and the second array does not have all the elements:

 array2: { 0 => "car1" 1 => "car4" 2 => "car5" } 

So, there are all possible categories (array1) and categories assigned by post (array2) (this is a WordPress thing).
I try to compare these arrays (but not with the array_diff() function, which led to the value of the elements instead of boolean) and get the result as true / false. Therefore, I want to compare all the values ​​of arrays and generate an array map, or perhaps use the array_combine() function to get such an array:

 result_array: { "car1": true, "car2": false, "car3": false } 

etc....
To output an array, it is important to have all categories and their results (true, false) for the message.
Is there an easy way to do this, or maybe this is a function that I could use?

+7
arrays php wordpress
source share
5 answers

Arrays are fun!

PHP has TON array functions, and therefore there are many potential solutions.

I came up with this as a personal task that does not use loops, filters or maps.

This solution uses array_intersect to find the values ​​that exist in both arrays, then array_values along with array_fill_keys to turn them into associative arrays filled with TRUE or FALSE , and finally array_merge to combine them into one array :

 $array1 = array( 0 => "car1", 1 => "car2", 2 => "car3", 3 => "car4", 4 => "car5"); $array2 = array( 0 => "car1", 1 => "car4", 2 => "car5" ); // Find all values that exist in both arrays $intersect = array_intersect( $array1, $array2 ); // Turn it into an associative array with TRUE values $intersect = array_fill_keys( array_values($intersect), TRUE ); // Turn the original array into an associative array with FALSE values $array1 = array_fill_keys( array_values( $array1 ), FALSE ); // Merge / combine the arrays - $intersect MUST be second so that TRUE values override FALSE values $results = array_merge( $array1, $intersect ); 

var_dump( $results ); leads to:

 array (size=5) 'car1' => boolean true 'car2' => boolean false 'car3' => boolean false 'car4' => boolean true 'car5' => boolean true 
+8
source share

array_map or array_combine does not actually return what you want. If you want to use array_map for the desired effect without writing a foreach loop, the desired result is given below. Note that you must pass array3 as a reference.

 <?php $array1 = array( 0 => "car1", 1 => "car2", 2 => "car3", 3 => "car4", 4 => "car5"); $array2 = array( 0 => "car1", 1 => "car4", 2 => "car5" ); $array3 = []; array_map(function($a) use ($array2, &$array3) { $array3[$a] = in_array($a, array_values($array2)); }, $array1); var_dump($array3); 

Output:

 array(5) { ["car1"]=> bool(true) ["car2"]=> bool(false) ["car3"]=> bool(false) ["car4"]=> bool(true) ["car5"]=> bool(true) } 
+3
source share

It is slow, but it should do what you want and be easy to understand.

 // Loop over the outer array which we're told has all the categories $result = array(); foreach($array1 as $sCar1) { $found = false; // Loop over the categories of the post foreach($array2 as $sCar2) { // If the current post category matches // the current category we're searching for // note it and move on if($sCar2 == $sCar1) { $found = true; break; } } // Now indicate in the result if the post has the current category $result[$sCar1] = $found; } 
+2
source share
 <?php //EXAMPLE 1 $array1 = [0 => "car1", 1 => "car2", 2 => "car3", 3 => "car4", 4 => "car5"]; $array2 = [0 => "car1", 1 => "car4", 2 => "car5"]; $resultArray = []; foreach ($array1 as $key => $val) { $resultArray[$val] = in_array($val, $array2); } var_dump($resultArray); ?> 

 <?php //EXAMPLE 2 $array1 = [0 => "car1", 1 => "car2", 2 => "car3", 3 => "car4", 4 => "car5"]; $array2 = [0 => "car1", 1 => "car4", 2 => "car5"]; $resultArray = []; $flipped = array_flip($array2); foreach ($array1 as $key => $val) { $resultArray[$val] = isset($flipped[$val]); } var_dump($resultArray); ?> 

RESULT:

 array (size=5) 'car1' => boolean true 'car2' => boolean false 'car3' => boolean false 'car4' => boolean true 'car5' => boolean true 
+2
source share

With array_count_values it is so simple:

 $results = array_map(function ($count) { return $count !== 1; }, array_count_values(array_merge($array1, $array2))); 

Thus, we combine the two arrays together, and then count the values. Then, if the number is not equal to 1, match the value with true , otherwise false . Interestingly, this will work no matter which array is shorter .

Here is a working demo .

Remember that the array_count_values function throws E_WARNING for every element that is not a string or integer . Thus, this approach will only work on arrays of strings and integers.

In addition, this approach may fail if the elements of the array are not unique, for this situation you need to apply array_unique to each array in advance.

+2
source share

All Articles