using if statement to compare array values

trying to get this random gen to work!

Page 1 of 1

14 Replies - 15765 Views - Last Post: 23 November 2008 - 01:23 PM Rate Topic: -----

#1 RudeDog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-November 08

using if statement to compare array values

Post icon  Posted 23 November 2008 - 12:19 AM

I would first like to thank everyone here at dream in code for the support and community. This is actually my first day programming php and I already feel like I have learned alot yet i know i have a ways to go. I will post my code first then explain what im trying to achieve after...

Now ill get to the code:

<?php
srand((float) microtime() * 10000000);
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
$health['Neo'] = "6";
$health['Morpheus'] = "5";
$health['Trinity'] = "4";
$health['Cypher'] = "3";
$health['Tank'] = "2";
echo $input[$rand_keys[0]] . " Has more health then\n";
echo $input[$rand_keys[1]] . "\n";
?>


Right now it is just displaying 2 random matrix names and always saying the first has more health regardless of the values ive set up. Im having trouble figuring out how to add the if statement in to compare values so the healthier matrix person is listed first.

would it be something like

if ($input[$rand_keys[0]]$health>$input[$rand_keys[1]]$health
echo $input[$rand_keys[0]] . " Has more health then\n";
echo $input[$rand_keys[1]] . "\n";

elseif ($input[$rand_keys[1]]$health>$input[$rand_keys[2]]$health
echo $input[$rand_keys[1]] . " Has more health then\n";
echo $input[$rand_keys[0]] . "\n";



i apologize if this question is really dumb but im still a noob with php and i appreciate your help.

thank you, -RD1

Is This A Good Question/Topic? 0
  • +

Replies To: using if statement to compare array values

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: using if statement to compare array values

Posted 23 November 2008 - 12:22 AM

View PostRudeDog1, on 23 Nov, 2008 - 01:19 AM, said:

if ($input[$rand_keys[0]]$health>$input[$rand_keys[1]]$health
echo $input[$rand_keys[0]] . " Has more health then\n";
echo $input[$rand_keys[1]] . "\n";

elseif ($input[$rand_keys[1]]$health>$input[$rand_keys[2]]$health
echo $input[$rand_keys[1]] . " Has more health then\n";
echo $input[$rand_keys[0]] . "\n";


They need close parenthesis. You only have the open one. You'll also need brackets.
Was This Post Helpful? 0
  • +
  • -

#3 RudeDog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-November 08

Re: using if statement to compare array values

Posted 23 November 2008 - 12:29 AM

ahh i see i did forget to close it with parenthesis.. By brackets do you mean put $health inside of [] ?
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: using if statement to compare array values

Posted 23 November 2008 - 12:37 AM

if ($input[$rand_keys[0]]$health>$input[$rand_keys[1]]$health) {
  echo $input[$rand_keys[0]] . " Has more health then\n";
  echo $input[$rand_keys[1]] . "\n";
}

elseif ($input[$rand_keys[1]]$health>$input[$rand_keys[2]]$health) {
  echo $input[$rand_keys[1]] . " Has more health then\n";
  echo $input[$rand_keys[0]] . "\n";
}


Brackets.
Was This Post Helpful? 0
  • +
  • -

#5 RudeDog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-November 08

Re: using if statement to compare array values

Posted 23 November 2008 - 12:51 AM

im getting this
Parse error: syntax error, unexpected T_VARIABLE in matrix.php on line 17
btw this is line 17. there must be something im doing wrong with $health
if ($input[$rand_keys[0]]$health>$input[$rand_keys[1]]$health) {


heres the whole php script so far:

<?php
srand((float) microtime() * 10000000);
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
$health['Neo'] = "6";
$health['Morpheus'] = "5";
$health['Trinity'] = "4";
$health['Cypher'] = "3";
$health['Tank'] = "2";
if ($input[$rand_keys[0]]$health>$input[$rand_keys[1]]$health) {
echo $input[$rand_keys[0]] . " Has more health then\n";
echo $input[$rand_keys[1]] . "\n";
}

else ($input[$rand_keys[1]]$health>$input[$rand_keys[2]]$health) {
echo $input[$rand_keys[1]] . " Has more health then\n";
echo $input[$rand_keys[0]] . "\n";
}
?>

Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: using if statement to compare array values

Posted 23 November 2008 - 01:02 AM

rand_keys isn't part of the input array is it? Plus you can't do a single comparison of 2 array elements in one statement like that.

Deal with rand_keys, then deal with health separately . They can't be the same item.
Was This Post Helpful? 0
  • +
  • -

#7 RudeDog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-November 08

Re: using if statement to compare array values

Posted 23 November 2008 - 01:27 AM

im sorry no2pencil, i appreciate your help but i am puzzled
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: using if statement to compare array values

Posted 23 November 2008 - 01:31 AM

if ($input[$rand_keys[0]]$health>$input[$rand_keys[1]]$health) {



What is health in this statement?

You need to treat health & rand_keys separately.
if ($input[$health]>$input[$health]) {



if ($input[$rand_keys]>$input[$rand_keys]) {



Maybe you can show me what values rand_keys holds, or how you are treating them, I might have a better, overall picture, as to what you are trying to do.
Was This Post Helpful? 1
  • +
  • -

#9 RudeDog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-November 08

Re: using if statement to compare array values

Posted 23 November 2008 - 01:55 AM

my goal is to be able to assign a value to each person, then when 2 of them are randomly drawn i can sort them having the healthier one display first. so if morpheus(5) and cypher(3) were drawn it would display

"Morpheus has more health then Cypher."

Similar to the classic card game war, but in a very basic layout, and i do mean basic. I am trying to compare them and have the script recognize which person has the higher value.
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,594
  • Joined: 08-August 08

Re: using if statement to compare array values

Posted 23 November 2008 - 07:48 AM

If you're looking for "Morpheus has more health than Cypher" then you'd want something like:

if ($health[$morpheus[5]] > $health[$cypher[3]])
{
// Do something
}
Was This Post Helpful? 0
  • +
  • -

#11 RudeDog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-November 08

Re: using if statement to compare array values

Posted 23 November 2008 - 10:30 AM

Id want to have the comparisons made based on the health (higher=better), without manually typing every comparison if I can avoid it,

typing neo is greater than morpheus, neo>trinity, neo>and go down the list for each character , then starting over at the next and saying morpheus is greater then trinity, morpheus is greater then tank etc etc seems a little redundant esp if I was using 20+ people instead of just 5 like in this example. Can I assign the value to the name and have the comparison done automatically to save many lines of code?
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,594
  • Joined: 08-August 08

Re: using if statement to compare array values

Posted 23 November 2008 - 10:44 AM

That's what loops are for:
$somevalue = 5; // where 5 is the number of items in the arrays
for ($i=0; $i<$somevalue; $i++) 
 {
 if ($first_array[$i] > $second_array[$i])
   {
   // do something
   }
 }



Oh, and it's:
if something is true, then something else. A is greater than B.

It is not:
if something is true, then something else. A is greater then B.

They are two different words: then and than, and they have different meanings.

This post has been edited by CTphpnwb: 23 November 2008 - 10:48 AM

Was This Post Helpful? 0
  • +
  • -

#13 RudeDog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-November 08

Re: using if statement to compare array values

Posted 23 November 2008 - 12:37 PM

Right but how can I compare the $health of $input[$rand_keys[0] to the $health of $input[$rand_keys[1]. thats the part im getting stuck on is integrating it into the equation
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,594
  • Joined: 08-August 08

Re: using if statement to compare array values

Posted 23 November 2008 - 12:59 PM

You could do something like this:
<?php
$health = array();
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank","Trinity's twin");
$rand_keys = array_rand($input, 2);
$health['Neo'] = "6";
$health['Morpheus'] = "5";
$health['Trinity'] = "4";
$health['Cypher'] = "3";
$health['Tank'] = "2";
$health['Trinity\'s twin']="4";
if ((int)$health[$input[$rand_keys[0]]] > (int)$health[$input[$rand_keys[1]]])
	{
	$comparison = "is healthier than";
	} else
	if ((int)$health[$input[$rand_keys[0]]] < (int)$health[$input[$rand_keys[1]]])
		{
		$comparison = "is not as healthy as";
		} else
		{
		$comparison = "is exactly as healthy as";
		}
echo $input[$rand_keys[0]]." ".$comparison." ".$input[$rand_keys[1]].".";
?>


This post has been edited by CTphpnwb: 23 November 2008 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,594
  • Joined: 08-August 08

Re: using if statement to compare array values

Posted 23 November 2008 - 01:23 PM

By the way, you're assigning health values as text. You'll probably be better off if you use numbers:
<?php
$health = array("Neo" =>6.1,"Morpheus"=>5,"Trinity"=>4,"Cypher"=>3,"Tank"=>2,"Trinity's twin"=>4);
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank","Trinity's twin");
$rand_keys = array_rand($input, 2);
if ($health[$input[$rand_keys[0]]] > $health[$input[$rand_keys[1]]])
	{
	$comparison = "is healthier than";
	} else
	if ($health[$input[$rand_keys[0]]] < $health[$input[$rand_keys[1]]])
		{
		$comparison = "is not as healthy as";
		} else
		{
		$comparison = "is exactly as healthy as";
		}
echo $input[$rand_keys[0]]." ".$comparison." ".$input[$rand_keys[1]].".";
?>


This post has been edited by CTphpnwb: 23 November 2008 - 01:26 PM

Was This Post Helpful? 1

Page 1 of 1