Duplicate array entries
Posted: Wed Feb 03, 2010 7:44 am
Hi,
I have an array, below, which contains duplicate values.
$corp =array(
131,
114,
111,
110,
135,
157,
152,
241,
240,
117,
360,
112,
240,
241,
242,
);
The array elements correspond to page ID numbers. I have used a foreach loop to cycle through the array and return page IDs before and after the current one and create links for these pages. For example, when on page 114, links are created for page IDs 131 (previous) and 111(next).
The code is:
foreach $corp as $key=>$value){
if ($value==$pageID){
$previousPageID = $theArray [$key-1];
$nextPageID = $theArray [$key+1];
}
}
This works fine until the loop comes to the duplicate values(for example 241). I want to be able to able to return a previous value of 152 and a next value of 240 (ie either side of the first 241), but the loop continues on and returns a previous value of 240 and a next of 242 (ie using the second instance of 241).
Does anyone know how to get around this problem? Thanks in advance...
I have an array, below, which contains duplicate values.
$corp =array(
131,
114,
111,
110,
135,
157,
152,
241,
240,
117,
360,
112,
240,
241,
242,
);
The array elements correspond to page ID numbers. I have used a foreach loop to cycle through the array and return page IDs before and after the current one and create links for these pages. For example, when on page 114, links are created for page IDs 131 (previous) and 111(next).
The code is:
foreach $corp as $key=>$value){
if ($value==$pageID){
$previousPageID = $theArray [$key-1];
$nextPageID = $theArray [$key+1];
}
}
This works fine until the loop comes to the duplicate values(for example 241). I want to be able to able to return a previous value of 152 and a next value of 240 (ie either side of the first 241), but the loop continues on and returns a previous value of 240 and a next of 242 (ie using the second instance of 241).
Does anyone know how to get around this problem? Thanks in advance...