This is about the tenth time I've re-written this post cos I'm finding it treally hard to word logically
Ok, I've got an array, a very large one with 700+ elements in it.
I need to sort the array using ksort and then to run a foreach loop which will define a variable $wap for each element like this.
Code: Select all
foreach ($ToneArray as $n => $v) {
$key = key($ToneArray);
if ($key == $n) {
$wap = $i+1;
}
$i++;
next($ToneArray);
}This loop echo's a bunch of hyperlinks back onto the page each of which will contain a unique number which is what $wap was for (just the key number+1 but I couldn't just extract it straight from the array since I've assigned my own keys) like this.
Code: Select all
arsort($ToneArray);
foreach (array_slice($ToneArray), 0, 25 as $n => $v) {
echo '<a href="index.php?location='.$n.'&mode=dl&wap='.$wap.'">Somelink</a><br>';
}I figured I need to open one loop, define $wap and then still inside this loop, do the resort and begin a new loop, then close both loops.
I just get a never ending loop where the 25 hyperlinks are just repeated over and over infinitely. It must be the way I'm nesting the loops inside each other so maybe somebody else would be able to give me some hints where I'm going wrong?
Here's the way I tried puting one loop inside the other:
Code: Select all
/*the array was defined by a function (which returns $ToneArray[$somevariable] = $anothervariable; and this definitely works*/
$ToneArray = listtones();
ksort ($ToneArray);
/*Start first loop*/
foreach ($ToneArray as $n => $v) {
/*Return $wap which is the numerical position in the array of each element*/
$key = key($ToneArray);
if ($key == $n) {
$wap = $i+1;
}
$i++;
next($ToneArray);
/*Resort the array and begin second loop still within the first one*/
arsort ($ToneArray);
foreach (array_slice($ToneArray, 0, 25) as $n => $v) {
echo '<a href="index.php?location='.$n.'&mode=dl&wap='.$wap.'">Somelinks</a><br>';
}
}Sorry it took so long to explain
Thanks