Page 1 of 1

Struggling with loops inside loops (Easy if you know how)

Posted: Sun Apr 18, 2004 12:50 pm
by Chris Corbyn
Hi,

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);
}
Once $wap is defined, I need to resort the array via arsort and then begin a new foreach loop on just elements 0-24 using array_slice.

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've tried defining $wap in it's own foreach loop and then creating the links in it's own loop too but the second loop does not read the variable $wap.

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>';
    }
}
Am I doing this all wrong? (Probably cos I'm kinda a newbie to most php).

Sorry it took so long to explain :oops:

Thanks :-)

Posted: Sun Apr 18, 2004 1:34 pm
by Chris Corbyn
By the way, I can't just define $wap in the foreach(array_slice($ToneArray, 0, 25) as $n => $v) loop because I need the numbers that $wap assigns to be assigned to the array as a whole in alphabetical order and then whichever elements happen to be in the sliced array need to maintain those values.

Posted: Sun Apr 18, 2004 2:12 pm
by McGruff
It's hard to work out exactly what's going on. First let me check:

- you have an array from which you wish to extract a range of keys
- you want to add these values to a string
- you also want to add a counter to the string

Code: Select all

<?php
function buildString($array) 
{        
    $string = '';
    $i = 0;
    foreach(array_keys($array) as $value)
    {
        $string .= '<a href="index.php?location=';
        $string .= $value;
        $string .= '&mode=dl&wap=';
        $string .= $i;
        $string .= '">Somelinks</a><br />'
        $i++;
    }
}
// in use
echo buildString(array_slice($my_array, 0, 25));

?>

Posted: Sun Apr 18, 2004 4:34 pm
by Chris Corbyn
Yeah that's right.

I've just rewritten bits of my code instead anyway to make it all come together without going to any great effort. I've just added extra bits to the array key separated by _ and then I've used an explode to get all the bits back out as little arrays in themselves. Quite useful actually cos you can store a lot of informatin in just one element so long as you separate each piece with a specific character and then explode can pull it all back out for you.

Got me thinking on the right lines though with using $wap = $i; $i++;

Thanks guys :-)

Posted: Sun Apr 18, 2004 11:25 pm
by McGruff
It's best not to store several bits of information in one element. Actually it's best not to store any information at all in the key. It's much harder to identify what's kept where and script edits could be harder than they need to be.

You could instead create a multi-dimensional array, with a separate key to store different bits of data (as the element value not the key).

Keys make more sense as meta data. Their job is to map the array structure (meta data) and so storing values (data) in keys can make the structure less clear.