0

Im looking the build a new array by finding the intersection of a given array of arrays.

This probably a simple set theory problem for most people but its kind of hard for me to wrap my head around.

So I have a keyed array:

$myArray = [
 'array1' => [A, B, C],
 'array2' => [A, B, C, D],
 'array3' => [A, B, C, D],
 'array4' => [D],
]

And the goal is to build an entirely new array keyed in with concatenated keys of the previous array if they have an intersection:

Basically like this:

 $newArray = [
   'array1,array2,array3' => [A, B, C],
   'array2,array3,array4' => [D]
  ]

Since array1, array2, array3 have ABC in command they will form as one keyed array, same as array2,array3,array4 and so on.

I have tried to experiment with array_intersect but it seems like its not the one that im looking for or perhaps I solving it in a really wrong way. Any ideas is much appreciated.

$myArray = [
 'array1' => [A, B, C],
 'array2' => [A, B, C, D],
 'array3' => [A, B, C, D],
 'array4' => [D],
];


$newArray = [];
$keys = array_keys($myArray);

for($i = 0, $size = count($myArray); $i < $size; $i++){
  for($k = $i; $k < $size; $k++){
      print_r(array_intersect($myArray[$keys[$i]], $myArray[$keys[$k]]));    
   }
}

  //Foal 
  // 'array1,array2,array3' => [A, B, C]
  // 'array2,array3,array4' => [D]
2
  • The question is not completely defined. Shouldn't $newArray also contain the key 'array1,array4' with value [] and the key 'array2,array3' with value [A, B, C, D] etc? Commented Aug 7, 2017 at 19:44
  • array2,array3 is not included since all of its elements exist in already in array1, array2, array3 and array2,array3,array4, although in set theory an empty set is also considered a new entry Commented Aug 7, 2017 at 19:52

1 Answer 1

2

This code will give the desired array as output.

$myArray = [
 'array1' => ['A', 'B', 'C'],
 'array2' => ['A', 'B', 'C', 'D'],
 'array3' => ['A', 'B', 'C', 'D'],
 'array4' => ['D'],
];
// Define new arrays
$newArray = [];
$finalArray = [];

// loop the array and create a new array with values like (A,B,C..) as keys 
foreach($myArray as $key=>$value){
    foreach($value as $k=>$v){
        // if already value exists append new value with comma
        $newArray[$v] = isset($newArray[$v]) ? $newArray[$v].','.$key:$key;
    }
}
foreach($newArray as $key=>$value){
        $finalArray[$value][] = $key;

}
print_r($finalArray);

Out put is

Array
(
    [array1,array2,array3] => Array
        (
            [0] => A
            [1] => B
            [2] => C
        )

    [array2,array3,array4] => Array
        (
            [0] => D
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

That is impressive,! Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.