fert Posted March 21, 2007 Share Posted March 21, 2007 $lines=explode(";",$_POST['names']); //$names=array(); for($count=0;$count<count($lines);$count++); { $temp=explode("|",$lines[$count]); $names[$temp[0]]=$temp[1]; } $names=asort($names); $num=rand(0,$names[0]); echo $num; echo "<pre>"; print_r($names); echo "</pre>"; $win=0; while($win==0) { foreach($names as $key=>$value) { if($value==$num) { die("{$key} has won with the number: {$value}"); } } $num=rand(0,$names[0]); } This code is suppose to take a string that looks like this name|number; name|number; name|number and repeat until one of the people has a number that matches the random number, but I keep getting errors about how $names isn't an array, but I don't see what's wrong with this code. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted March 21, 2007 Share Posted March 21, 2007 This should work. Note that asort doesn't need to be assigned to a variable, it is executed on an array, and returns true or false. Also note that you could potentially be in the while loop for a VERY long time if there is more than a couple of elements in the $names array using your current code. $names = "name|number; name|number; name|number"; $lines = explode(";",$_POST['names']); foreach ($lines as $line) { $line = explode("|", trim($line)); $names[$line[0]] = $line[1]; } asort($names); $num = rand(0, $names[0]); echo $num; echo "<pre>" . print_r($names, true) . "</pre>"; $win = 0; while($win == 0) { foreach ($names as $key => $value) { if ($value == $num) { echo "{$key} has won with the number: {$value}"; $win = 1; } } $num = rand(0,$names[0]); } Quote Link to comment Share on other sites More sharing options...
fert Posted March 21, 2007 Author Share Posted March 21, 2007 Nope still gives the same errors Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted March 21, 2007 Share Posted March 21, 2007 $text = "name0|number0; name1|number1; name2|number2"; $lines = explode(";",$text); foreach ($lines as $line) { $line = explode("|", trim($line)); $names[$line[0]] = $line[1]; } asort($names); $keys = array_keys($names); $num = rand(0, count($names)); echo "<pre>" . print_r($names, true) . "</pre>"; echo "{$names[$keys[$num]]} has won with the number: {$num}"; Quote Link to comment Share on other sites More sharing options...
fert Posted March 21, 2007 Author Share Posted March 21, 2007 that gives me Warning: asort() expects parameter 1 to be array, string given in /home/content/z/z/i/zzieba/html/choser.php on line 25 Warning: array_keys(): The first argument should be an array in /home/content/z/z/i/zzieba/html/choser.php on line 26 e|56; you|67; mo|145; has won with the number: 0 Warning: asort() expects parameter 1 to be array, string given in /home/content/z/z/i/zzieba/html/choser.php on line 40 Warning: array_keys(): The first argument should be an array in /home/content/z/z/i/zzieba/html/choser.php on line 41 e|56; you|67; mo|145; has won with the number: 0 Quote Link to comment Share on other sites More sharing options...
fert Posted March 21, 2007 Author Share Posted March 21, 2007 bump Quote Link to comment Share on other sites More sharing options...
Barand Posted March 21, 2007 Share Posted March 21, 2007 try <?php $postnames = 'a|21;b|42;c|66'; $lines=explode(";",$postnames); $names=array(); foreach ($lines as $line) { $temp=explode("|",$line); $names[$temp[0]]=$temp[1]; } asort($names); echo "<pre>"; print_r($names); echo "</pre>"; $win = array_rand($names); echo "$win wins with number $names[$win]"; ?> Quote Link to comment Share on other sites More sharing options...
fert Posted March 21, 2007 Author Share Posted March 21, 2007 Thanks, that works perfect Quote Link to comment 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.