Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

PHP Intersect Array but not NULL Array

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

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

1 Reply

0 votes
by (71.8m points)

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;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...