我创建操纵CSV文件PHP类的多维数组。 作为班级的一部分,我有一个功能,允许过滤数据showOnlyWhere
。但是,我收到331行(foreach
声明行)上的此错误Invalid argument supplied for foreach()
。我试着添加global $arr;
但这没有奏效。我将如何解决它?过滤基于另一个数组
$this -> rows
是包含所有CSV数据的多维阵列。
$arr
的格式为:
$key=>$val array(
$key = Column Name
$val = value that column should contain
)
下面是showOnlyWhere
功能
function showOnlyWhere($arr)
{
if($this->showOnlyWhere == true){
$rows = $this->filteredRows;
}
else{
$rows = $this->rows;
}
$filter = function ($item){
global $arr; // didn't work
foreach($arr as $chkCol => $chkVal){
if ($item[$arr[$chkCol]] != $chkVal){
return false;
break(3);
}
}
return true;
};
$this->filteredRows = array_filter($rows,$filter);
$this->showOnlyWhere = true;
}
我认为错误可能有一些做的匿名函数 - 但我真的不知道。
IM过滤'$ rows' – jamesmstone