【问题标题】:Issue with array_intersectarray_intersect 的问题
【发布时间】:2016-02-17 16:57:39
【问题描述】:

我在这段代码中有问题。我必须有计数大于0的表之间的交集。然后我使用了函数array_interset()但是当我使用条件来计数时,这段代码有问题:

$iheb1 = 
array_intersect( 
    if(count($tab0)>0) { $tab0, }
    if(count($tab1)>0) { $tab1, }
    if(count($tab2)>0) { $tab2, } 
    if(count($tab3)>0) { $tab3, }
    if(count($tab4)>0) { $tab4, }
    if(count($tab5)>0) { $tab5, }
    if(count($tab6)>0) { $tab6 }
);  

【问题讨论】:

  • 不,你的代码有很多问题。
  • 请修改您的代码格式、拼写错误并添加说明为什么您的代码无法正常工作,以及您遇到的错误。另外,您的标题是法语,请使用更具描述性的英文标题

标签: php


【解决方案1】:

这里有很多问题。最重要的是你在 array_intersect 参数中的 if 语句。

试试这个:

首先将所有数组添加到一个或多个数组中(我使用了 $full_array)。

$array_to_test = array();

foreach($full_array as $arr){
    if(count($arr)>0){
        array_push($array_to_test, $arr);
    }
}
$iheb1 = array_reduce($array_to_test,function(&$a,$b) {$a = array_intersect($a,$b);},Array());

我找到了您的similar question,这似乎有效。

【讨论】:

  • 我喜欢在长度>0的数组之间有交集
  • 它确实回答了他为什么会出错。最初发布时,没有关于最终目标的更多信息,所以这是唯一要回答的问题。
  • 我喜欢确定只有在表中计数>0的交叉点
  • 我使用了这个代码 $array_to_test = array(); $fullArray = array($tab0,$tab1,$tab2,$tab3,$tab4,$tab5,$tab6,$tab7); foreach($full_array as $arr){ if(count($arr)>0){ array_push($array_to_test, $arr); } } $iheb1 = array_reduce($array_to_test,function(&$a,$b) {$a = array_intersect($a,$b);},Array());
  • 我们需要更多信息。它给你一个错误吗?您还经历了哪些其他调试步骤来测试它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
  • 2017-05-09
相关资源
最近更新 更多