我有一個網址列表,每個頁面都是一個特定的類別:PHP數組和嵌套循環
http://www.site.com/category-1/page.html
http://www.site.com/category-2/page.html
http://www.site.com/category-3/page.html
在每一頁上都讓我們說4項。我想提取頁面上的每個項目,併爲其分配它的IE
category-1_ITEM - CAT-1
category-1_ITEM - CAT-1
category-1_ITEM - CAT-1
category-1_ITEM - CAT-1
category-2_ITEM - CAT-2
category-2_ITEM - CAT-2
category-2_ITEM - CAT-2
category-2_ITEM - CAT-2
category-3_ITEM - CAT-3
category-3_ITEM - CAT-3
category-3_ITEM - CAT-3
category-3_ITEM - CAT-3
我想這將是非常簡單的相應類別數量,但現在我不得不面對明顯的循環問題,這裏的代碼,我已刪除針對簡單起見,所有無關行:
$urls = array(
"http://www.site.com/category-1/page.html",
"http://www.site.com/category-2/page.html",
"http://www.site.com/category-3/page.html"
);
foreach ($urls as $url) {
//Load Page, find items
foreach($items as $item) {
preg_match('#http\:\/\/www\.site\.com\/(.*?)\/page\.html#is',$url,$result);
switch ($result[1]){
case "category-1": $cat = 'CAT-1'; break;
case "category-2": $cat = 'CAT-2'; break;
case "category-3": $cat = 'CAT-3'; break;
}
echo $item . ' - ' . $cat . '<br>';
}
}
這就是它輸出:
category-1_ITEM - CAT-1
category-1_ITEM - CAT-1
category-1_ITEM - CAT-1
category-1_ITEM - CAT-1
category-1_ITEM - CAT-2
category-1_ITEM - CAT-2
category-1_ITEM - CAT-2
category-1_ITEM - CAT-2
category-2_ITEM - CAT-2
category-2_ITEM - CAT-2
category-2_ITEM - CAT-2
category-2_ITEM - CAT-2
category-1_ITEM - CAT-3
category-1_ITEM - CAT-3
category-1_ITEM - CAT-3
category-1_ITEM - CAT-3
category-2_ITEM - CAT-3
category-2_ITEM - CAT-3
category-2_ITEM - CAT-3
category-2_ITEM - CAT-3
category-3_ITEM - CAT-3
category-3_ITEM - CAT-3
category-3_ITEM - CAT-3
category-3_ITEM - CAT-3
什麼我做錯了任何想法?我有一種感覺,這是一個簡單的錯誤,我只是沒有看到它。
這些傢伙可能是對的,你需要添加代碼「//加載頁面,找到物品」以獲得真正的答案。 – 2010-08-26 21:40:23