Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to generate array as a result of comparison 2 arrays: 1st array has more elements such as:

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

and 2nd array has not all elements:

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

So there are all possible categories (array1) and categories assigned to post (array2) (it's a WordPress thing).
I am trying to compare that arrays (but not with array_diff() function which gave me elements values instead boolean) and get result as true/false. So I want to compare each values of arrays and generate array map or maybe using array_combine() function to get array like that:

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

etc...
It is important to output array have all categories and their results (true, false) for post.
Is there any simple way to do it or maybe is it function which I could use?

like image 567
Luqsus Avatar asked Nov 23 '16 23:11

Luqsus


People also ask

How do I compare two arrays of arrays?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.

How do you compare two arrays to find matches?

JavaScript finding non-matching values in two arrays The code will look like this. const array1 = [1, 2, 3, 4, 5, 6]; const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const output = array2. filter(function (obj) { return array1. indexOf(obj) === -1; }); console.


1 Answers

Arrays are fun!

PHP has a TON of array functions, and so there's lots of potential solutions.

I came up with this one as a personal challenge, which uses no loops, filters, or maps.

This solution uses array_intersect to find values that exist in both arrays, then array_values along with array_fill_keys to turn them into associative arrays populated with TRUE or FALSE, and finally array_merge to put them together into a single 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 ); results in:

array (size=5)
  'car1' => boolean true
  'car2' => boolean false
  'car3' => boolean false
  'car4' => boolean true
  'car5' => boolean true
like image 141
random_user_name Avatar answered Oct 21 '22 16:10

random_user_name