Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have five 2-D Arrays, (You can assume it like five spreadsheets), I want to take the intersection of the five (I want the common rows of all five in a new spreadsheet) but now I find out that one of the array is a NULL Array, (I find that instead of a spreadsheet, it's just a blank piece of paper), so now I want to ignore it and Intersect the rest of the Arrays... Please suggest a method of doing that...

P.S. - I know it is against the property of intersection because we all know that Intersection of anything with NULL is NULL, but that is not what I want...

P.S. - I also don't know how many Arrays can be empty, I just assumed it to be 1 as an Example, It can be 2, 3, 4 or Even 5... Yes, If it is 5, then returning NULL is perfect but not in any other case... Suppose if 4 Arrays are NULL, then It should return the 5th Array...

Language Used : PHP

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
296 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use array_filter() and a custom implementation of in_array() to check if your array is null

Solution :

$array1 = array(6,  10, 11, 12);
$array2 = array(6, 741, 18, 9, 110, 11, 12);
$array3 = array(8, 10, 11, 20);
$array4 = null;
$array5 = array(9, 10, 11, 12);


function in_array_custom($item, $array)
{
    if($array === null){
        return true;
    }
    return in_array($item, $array);
}

function intersect($item)
{
    global $array2;
    global $array3;
    global $array4;
    global $array5;
    return in_array_custom($item, $array2) && in_array_custom($item, $array3) && in_array_custom($item, $array4) && in_array_custom($item, $array5);
}

print_r(array_filter($array1, "intersect")); 

Live example

I share the global solution :

<?php

$arrays = array(
    array(6,  10, 11, 12),
    array(6, 741, 18, 9, 110, 11, 12),
    array(8, 10, 11, 20),
    null,
    array(9, 10, 11, 12)
);

function in_array_custom($item, $array)
{
    if($array === null){
        return true;
    }
    return in_array($item, $array);
}

function in_arrays($item, $arrays)
{
    foreach($arrays as $array)
    {
        if(!in_array_custom($item, $array)) {
            return false;
        }
    }
    return true;
}

function intersect($item)
{
    global $arrays;
    return in_arrays($item, $arrays);
}

print_r(array_filter($arrays[0], "intersect"));

Live example


Here, there is one little issue, that If the first array ($array1) is null, then the code will not work, but that issue can be resolved by taking $array1 as a Union of all the SIX Arrays, (5 Arrays and 1 Union Array), And the Data is shifted to the next array, i.e $array2 now holds the data of $array1, $array3 = $array2 and so on...

P.S. - Union of Arrays can be done like this $array1 = $array2 + $array3 + $array4 + $array5 + $array6;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...