我发布一个文本区域到我的php脚本,然后我做一个循环的文本区域的每一行,然后使用从文本区域循环的数据另一次。然后,我试图为每个人设置一个数组,并在最后检索所有这些数组。然而,我的代码给我错误:
if (isset($_POST['submit'])) {
$entries = array();
$text = trim($_POST['facebookpage']);
$text = explode("'n", $text);
foreach ($text as $line) {
$data = $html2->find("table.profileInfoTable");
$text2 = trim($data[0]);
$text2 = explode("<tr>", $text2);
foreach ($text2 as $line) {
if (strpos($line, 'Location') !== false) {
$location = $line;
}
}
$data1 = $html2->find("table.profileInfoTable");
$text2 = trim($data1[0]);
$text2 = explode("<tr>", $text2);
foreach ($text2 as $line) {
if (strpos($line, 'Email') !== false) {
$email = $line;
}
}
$mainarray = array("Email" => $email, "Location" => $location);
array_push(($mainarray),$entries);
}
var_dump($entries);
}`
错误是:
Fatal error: Only variables can be passed by reference in /home2/statonme/public_html/scraper.php on line 61
通常,PHP的错误消息会让你很好地了解你的脚本出了什么问题。
我不知道你的脚本中的"第61行"在哪里,但我相信如果你把任何东西放在括号里,它被认为是一个"方程"…这意味着($main_array)
不是一个变量…你不能改变方程。你只能修改变量
尝试在array_push(($mainarray),$entries);
中移除$main_array
周围的括号(使其为:array_push($mainarray, $entries);
)并请返回结果。
我认为你把array_push
的论点倒了。你要添加的数组应该是第一个参数,其余的参数是要添加的元素。所以应该是:
array_push($entries, $mainarray);
或者更简单:
$entries[] = array("Email" => $email, "Location" => $location);