Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中使用数组生成函数_Php_Arrays_Function - Fatal编程技术网

如何在PHP中使用数组生成函数

如何在PHP中使用数组生成函数,php,arrays,function,Php,Arrays,Function,我试图在PHP中创建一个带有数组的函数。使用名为simple_html_dom.php的库从站点提取数据 我已经编写了一段代码,它的工作方式与预期的完全相同,但它很容易重复。所以我想用数组创建一个函数 在这里,我试图将代码转换成一个函数,这段代码可以工作 include('simple_html_dom.php'); $pos = 0; $food = 1; $col_num = array(); $col_food = array(); $html = file_get_html('we

我试图在PHP中创建一个带有数组的函数。使用名为simple_html_dom.php的库从站点提取数据

我已经编写了一段代码,它的工作方式与预期的完全相同,但它很容易重复。所以我想用数组创建一个函数

在这里,我试图将代码转换成一个函数,这段代码可以工作

include('simple_html_dom.php');

$pos = 0;
$food = 1;

$col_num = array();
$col_food = array();

$html = file_get_html('website');

for($i = 0;$i<220;$i+=11){

    // Extract all text from a given cell
    //insert data into the array to the field it belongs to     
    array_push($col_num, $html->find('td', $i)->plaintext);
    array_push($col_food, $html->find('td', $food)->plaintext);

            $food +=  11;
 }


 for($row = 0;$row<=19;$row++){

echo $col_num[$row].$col_food[$row]."<br>";

}
include('simple_html_dom.php');
$pos=0;
$food=1;
$col_num=array();
$col_food=array();
$html=file_get_html(“网站”);
对于($i=0;$ifind($td',$i)->纯文本);
数组推送($col\u food,$html->find('td',$food)->明文);
$food+=11;
}
对于($row=0;$row明文);
}

对于($rows=0;$rows而言,问题在于$html超出范围。您需要将$html传递到getcoleachrow函数中,例如:

function getcoleachrow($col = array(), $value, $html){

   for($value=$value;$value<220;$value+=11){

    array_push($col, $html->find('td', $value)->plaintext);
   }

   for($rows = 0;$rows<=19;$rows++){

    echo $col[$rows]."<br>";
   }

}

getcoleachrow($col_num, $num, $html);
函数getcoleachrow($col=array(),$value,$html){ 对于($value=$value;$valuefind('td',$value)->纯文本); }
对于($rows=0;$rows您需要在函数中添加
global$html;

function getcoleachrow($col = array(), $value){

   global $html;

   for($value=$value;$value<220;$value+=11){

    array_push($col, $html->find('td', $value)->plaintext);
   }

   for($rows = 0;$rows<=19;$rows++){

    echo $col[$rows]."<br>";
   }

}
函数getcoleachrow($col=array(),$value){ 全球$html; 对于($value=$value;$valuefind('td',$value)->纯文本); }
对于($rows=0;$rowst这与DOM毫无关系,而且似乎每个人都忘记了不应该在可选参数后加上必需的参数。可以将其切换或删除“=array()”。每个人都始终使用全局:-)进行向下投票全局变量在这种情况下是不好的做法。我不知道函数的其余部分是否正常工作,因为我不知道_get_html()返回的文件是什么;)很高兴能提供帮助!请您将此标记为答案(如果您愿意,请向上投票!)?
function getcoleachrow($col = array(), $value){

   global $html;

   for($value=$value;$value<220;$value+=11){

    array_push($col, $html->find('td', $value)->plaintext);
   }

   for($rows = 0;$rows<=19;$rows++){

    echo $col[$rows]."<br>";
   }

}