firelior Posted September 22, 2006 Share Posted September 22, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/21671-array-problem/ Share on other sites More sharing options...
Daniel0 Posted September 22, 2006 Share Posted September 22, 2006 Just do this: [code]foreach($elements as $key => $val){ echo "<option>$val</option>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21671-array-problem/#findComment-96775 Share on other sites More sharing options...
firelior Posted September 22, 2006 Author Share Posted September 22, 2006 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" Quote Link to comment https://forums.phpfreaks.com/topic/21671-array-problem/#findComment-96776 Share on other sites More sharing options...
Daniel0 Posted September 22, 2006 Share Posted September 22, 2006 [code]foreach($elements as $val){ foreach($val as $key => $val) { echo "<option>$val</option>"; }}[/code]Then this should do it. Quote Link to comment https://forums.phpfreaks.com/topic/21671-array-problem/#findComment-96781 Share on other sites More sharing options...
firelior Posted September 22, 2006 Author Share Posted September 22, 2006 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 problema little onehow do I test if one is an integer(number) and one is a string? Quote Link to comment https://forums.phpfreaks.com/topic/21671-array-problem/#findComment-96786 Share on other sites More sharing options...
Daniel0 Posted September 22, 2006 Share Posted September 22, 2006 To check for numbers: is_int or is_numeric (the latter one is probably the best).To check for strings: is_string Quote Link to comment https://forums.phpfreaks.com/topic/21671-array-problem/#findComment-96789 Share on other sites More sharing options...
Barand Posted September 22, 2006 Share Posted September 22, 2006 Are you sure you don't want this[code]<?phpecho "<select name='test'>\n";foreach ($elements as $val){ echo "<option value='{$val['id']}'>{$val['name']}</option>\n";}echo "</select>\n";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21671-array-problem/#findComment-96892 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.