Jump to content

Archived

This topic is now archived and is closed to further replies.

firelior

array problem

Recommended Posts

Hi,
I got this array:
[code] while($row = mysql_fetch_assoc($result))
{
$elements[] = $row;
}
        return $elements;
[/code]
now I want to extract it.
I don't have the database or anything I tryed this:
[code] $option=$elements;
foreach ($option as $key => $val){
foreach ($option as $val){
echo "<option>$val</option>";
}
}[/code]

Share this post


Link to post
Share on other sites
Just do this: [code]foreach($elements as $key => $val)
{
echo "<option>$val</option>";
}[/code]

Share this post


Link to post
Share on other sites
hi again, well it doesn't work,
because elements = Array ( [ 0 ] => Array ( [id] => 1 [name] => firelior ) )
and what you did is return an array not "1" or "firelior"

Share this post


Link to post
Share on other sites
[code]foreach($elements as $val)
{
foreach($val as $key => $val)
{
echo "<option>$val</option>";
}
}[/code]

Then this should do it.

Share this post


Link to post
Share on other sites
thanks, never mind i solved it:
[code] foreach ($option as $key => $val){
foreach ($val as $key => $val1){

echo "<option>$val1</option>";
}
}[/code]

but i got another problem
a little one
how do I test if one is an integer(number) and one is a string?

Share this post


Link to post
Share on other sites
To check for numbers: is_int or is_numeric (the latter one is probably the best).
To check for strings: is_string

Share this post


Link to post
Share on other sites
Are you sure you don't want this

[code]<?php
echo "<select name='test'>\n";
foreach ($elements as $val){
echo "<option value='{$val['id']}'>{$val['name']}</option>\n";
}
echo "</select>\n";
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.