unidox Posted July 10, 2008 Share Posted July 10, 2008 I am trying to get this to work, but its not... Whats wrong? <?php $codes_used = array( array("Unidox", "0"), array("Ro0t", "0"), //<newuser> ); function find_user($user) { $out = false; $cnt = count($codes_used); $i = 0; while ($i < $cnt) { if ($user == $codes_used[$i][0]) { $out = true; } $i++; } return $out; } if (find_user("Unidox")) { echo "True"; } else { echo "False"; } ?> Link to comment https://forums.phpfreaks.com/topic/114025-why/ Share on other sites More sharing options...
aim25 Posted July 10, 2008 Share Posted July 10, 2008 Use a foreach loop. Link to comment https://forums.phpfreaks.com/topic/114025-why/#findComment-586037 Share on other sites More sharing options...
aim25 Posted July 10, 2008 Share Posted July 10, 2008 Here is a good example. Play with it a bit. <?php $cds = array( 'Christina Aguilera'=>array( 'Impossible', 'Beautiful', 'Dirrty' ), 'Pink'=>array( 'Just like a pill', 'Family potrait', 'Missundaztood' ), 'Kelly Rowland'=>array( 'Stole', 'Obsession', 'Past 12' ) ); echo '<p><b>How many CDs in our collection?</b><br /> There are '.sizeof($cds)." CDs in our collection.</p>\n"; echo "<ol>\n"; foreach( $cds as $singer=>$songs ) { echo ' <li><b>'.$singer."</b>.<br />\n"; echo " <em>Songs</em>: </li>\n"; echo " <ul>\n"; asort( $songs ); // sorts the list of songs foreach( $songs as $song ) { echo ' <li>'.$song.".</li>\n"; } echo " </ul>\n"; } echo "</ol>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/114025-why/#findComment-586038 Share on other sites More sharing options...
unidox Posted July 10, 2008 Author Share Posted July 10, 2008 I tried this but still not working .... <?php $codes_used = array( array("Unidox", "0"), array("Ro0t", "0"), //<newuser> ); function find_user($user) { $out = false; $i = 0; foreach ($codes_used as $cud) { if ($user == $cud[0]) { $out = true; } $i++; } return $out; } if (find_user("Unidox")) { echo "True"; } else { echo "False"; } ?> Link to comment https://forums.phpfreaks.com/topic/114025-why/#findComment-586041 Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 $codes_used does not exist within your function. <?php $codes_used = array( array("Unidox", "0"), array("Ro0t", "0"), //<newuser> ); function find_user($user,$codes_used) { foreach ($codes_used as $cud) { if ($user == $cud[0]) { return true; } } return false; } if (find_user('Unidox',$codes_used)) { echo "found\n"; } else { echo "not found\n"; } ?> I also cleaned your function of un required code. Link to comment https://forums.phpfreaks.com/topic/114025-why/#findComment-586058 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.