Forum Moderators: coopster

Message Too Old, No Replies

Looping through an array and using the results.

         

createErrorMsg

4:11 am on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function build_menu() {
$all_li = create_list_items();
$menu = '<ul id="navbar">';
reset($all_li);
while($li = each($all_li)) {
$menu .= $li;
}
$menu .= '</ul>';
echo($menu);
}

I'm trying to move through each item in the #all_li array and add it's contents to the $menu variable. $all_links is filled with string values from the function create_list_items().

I tried (above) using each(), but the output from the function is "ArrayArrayArray" and this has me confused. I think it's giving me this because each() creates an array which includes key values (yes?), which means each() is not the method I need to use.

Can anybody advise me on a method I can use to loop through the $all_links array and add each string to the $menu variable? So far I've tried a for loop, foreach, even attempted an array_walk(), but haven't had any success.

Thanks in advance for any help.

cEM

JamesRock

4:15 am on Oct 21, 2004 (gmt 0)

10+ Year Member



well without knowing the structure of your array it is a little difficult, if my suggestion below works please send me the results of


var_dump($all_li);

run after the line with "$all_li = create_list_items(); " in it.

But anyway, try this solution and see what you get:


function build_menu() {
$all_li = create_list_items();
$menu = '<ul id="navbar">';
reset($all_li);
foreach ($li AS $key => $value) {
$menu .= $key . ': ' . $value;
}
$menu .= '</ul>';
echo($menu);
}

this mike work but its imperative that I know the structure of $all_li in order to be able to help you.

mincklerstraat

8:14 am on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



James has got it here (welcome to webmasterworld) [webmasterworld.com], James

From your description here, it's possible you don't want the 'key' part - if this code outputs too much stuff, on the second line that changes $menu, use:

$menu .= $value;

createErrorMsg

7:45 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you both for your replies.

The array $all_li holds a string in each array 'cell' that is the html code for a list item with a link inside...

<li><a href="#" id="">Link Text</a></li>

I think the problem was that each() adds a $key element to the array, so simply adding each array value was adding another array, not a single string.

I did manage to get the script working prior to seeing your posts by using a for loop...

#all_li = create_list_items();
$menu = '<ul id="navbar">';
for ($i;$i<count($all_li);$i++) {
$menu .= $all_li[$i];
}

...

Does that seem like a reliable enough solution to you, or should I go with your suggestion? I'm still on the learning curve for PHP and want to try to use the most stable/reliable/secure methods possible.

Thanks again for your help.
cEM

mincklerstraat

6:55 am on Oct 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This'll do you just fine.

In the future, though, try using foreach: [be2.php.net...]

It is dang easy to use. It just lets you loop through an array using each value, or key and value if you need that (the things you're calling cells here). while() will do this also in the construction you've mentioned with count(), so your version should work just fine, but that actually introduces a tiny bit more complexity into the whole bit, and foreach() is also known for being a really fast way of looping, which is what you want since looping in general is one of the things that gets your code bogged down.

createErrorMsg

1:51 pm on Oct 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



minklerstraat, thanks again for the great post. It's always nice to get a response that both helps and explains. It's so much easier to understand what to do when you understand what is happening, as well.

cEM