PHP arrays & nested loops

aGreebarnita24

New Member
I have a list of URLs, each page is a specific category:\[code\]http://www.site.com/category-1/page.htmlhttp://www.site.com/category-2/page.htmlhttp://www.site.com/category-3/page.html\[/code\]On each page are let's say 4 items. I want to extract each item on each page and assign it it's corresponding category number i.e.\[code\]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 \[/code\]I figured this would be pretty straightforward but now I'm having to deal with apparent looping issues, here's the code, I've removed all irrelevant lines for simplicity's sake:\[code\]$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 itemsforeach($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>';}}\[/code\]This is what it outputs:\[code\]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-3category-1_ITEM - CAT-3 category-2_ITEM - CAT-3 category-2_ITEM - CAT-3 category-2_ITEM - CAT-3category-2_ITEM - CAT-3 category-3_ITEM - CAT-3 category-3_ITEM - CAT-3 category-3_ITEM - CAT-3category-3_ITEM - CAT-3 \[/code\]Any ideas on what I'm doing wrong? I have a feeling it's a simple mistake, I'm just not seeing it.
 
Back
Top