【发布时间】:2013-12-16 01:40:45
【问题描述】:
我对 PHP 还很陌生,并且遇到了数组问题。我刚刚设置了我的函数,其中包含一个数组。一切正常,除了当我在函数外部调用我的数组时,内部似乎什么都没有,(我的最后一个 for 循环没有打印任何内容)错误显示“注意:未定义的偏移量:每个数字”。
代码如下:
$col_num = array();
// `getcoleachrow()`: function to get each data in each column row
function getcoleachrow ($col = array(), $value, $html){
// 220 cells in the table
for ($value=$value;$value<220;$value+=11){
//gets the data from a cell then turn it to text and stores it in an array selected
array_push($col, $html->find('td', $value)->plaintext);
}
// 20 rows and 20 data from the table in an array
for ($rows = 0;$rows<=19;$rows++) {
//print out array
echo $col[$rows];
}
} // getcoleachrow
// Call the `getcoleachrow()` function
getcoleachrow($col_num, $pos, $html);
//getcoleachrow($col_team, $team);
// Goes through every row
for($row = 0;$row<=19;$row++){
echo $col_num[$row];
}
最后一个 for 循环是空的,但 getcoleachrow($col_num, $pos, $html); 会打印出我想要的每一个。
【问题讨论】:
-
你可以 print_r($arr) 而不是循环遍历它, echo "
" 也会很方便
-
你可以从函数中返回
$col或者让它通过引用传递。 -
谢谢你,绝对没有数据存储在函数之外
标签: php arrays function global-variables scope