2013-03-23 122 views
0

我將文本區域發佈到我的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 

回答

1

通常,PHP的錯誤信息給你的什麼錯你的腳本一個非常好的主意。

我不知道腳本中的'第61行'是什麼,但我相信如果你把任何東西放在括號裏面,它就被認爲是'等式'......這意味着($main_array)不是一個變量......你不能改變方程。你只能改變變量。

嘗試清除$main_array中的array_push(($mainarray),$entries);(使其成爲:array_push($mainarray, $entries);)的括號,然後返回結果。

+0

感謝您的答覆,已使得它做點什麼但是在瀏覽器窗口僅僅是卡住加載了。我想這是在我嘗試寫入數組的時候做的。 – 2013-03-23 07:45:09

+0

請轉到:http://simonstaton.co.uk/scraper.php並在http://www.facebook.com/MauiNuiBotanicalGardens中輸入表單然後單擊提交併查看它的作用 – 2013-03-23 07:47:36

+0

@SimonStaton我相信會發生這種情況,因爲你在之前的代碼中有一個無限循環。如果內存服務,PHP首先解析整個腳本,以瞭解它必須做什麼,並提醒你明顯的錯誤......它不知道該怎麼做。你的代碼現在在語法上是正確的,但這並不意味着你的代碼是完美無瑕的。 – 2013-03-23 07:50:10

1

我認爲你有反向的參數array_push。您要添加的數組應該是第一個參數,其餘參數是要添加的元素。因此,它應該是:

array_push($entries, $mainarray); 

或者更簡單地說:

$entries[] = array("Email" => $email, "Location" => $location); 
+0

啊,完全錯過了。我被錯誤消息佔據了,我沒有看到更大的圖片。對於+1。 – 2013-03-23 08:02:50