PHP相交数组而不是NULL数组 [英] PHP Intersect Array but not NULL Array
问题描述
我有五个二维数组(您可以假设它像五个电子表格一样),我想取五个的交集(我想要一个新电子表格中所有五个的共同行),但是现在我发现了数组之一是NULL数组,(我发现它不是电子表格,而只是一张空白的纸),所以现在我想忽略它并与其余的数组相交...请建议一种方法那...
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. -我知道这与交集的性质相反,因为我们都知道带有NULL的任何事物的交集都是NULL,但这不是我想要的...
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. -我也不知道有多少个数组可以为空,我只是假设它为1,可以为2、3、4或什至5 ...是的,如果为5,则返回NULL是完美,但在任何其他情况下都不行...假设4个数组为NULL,则它应该返回第5个数组...
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...
使用的语言:PHP
推荐答案
您可以使用 array_filter()和 in_array() >检查您的数组是否为空
You can use array_filter() and a custom implementation of in_array() to check if your array is null
解决方案:
$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"));
我分享了全局解决方案:
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"));
这里,有一个小问题,如果第一个数组($array1
)为空,则代码将不起作用,但是可以通过将$array1
作为所有SIX数组的并集来解决该问题. ,(5个数组和1个联合数组),然后将数据移至下一个数组,即$array2
现在保存了$array1
,$array3 = $array2
等数据……
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. -数组联合可以像这样$array1 = $array2 + $array3 + $array4 + $array5 + $array6;
P.S. - Union of Arrays can be done like this $array1 = $array2 + $array3 + $array4 + $array5 + $array6;
这篇关于PHP相交数组而不是NULL数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!