Jump to content

[SOLVED] Help with an array problem


fert

Recommended Posts

$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.

Link to comment
https://forums.phpfreaks.com/topic/43594-solved-help-with-an-array-problem/
Share on other sites

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]);
}

$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}";

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

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]";
?>

Archived

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

×
×
  • Create New...

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.