9 Replies - 2263 Views - Last Post: 18 November 2008 - 08:40 AM Rate Topic: -----

#1 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Determining largest VARIABLE, not VALUE

Posted 17 November 2008 - 02:06 PM

Hey guys,

max() is used to determine the largest value in an array. If more than 1 parameter is given, it will find the largest value out of the paramenters //php.net and WeberDev.net

echo max(1, 3, 5, 6, 7); // 7
echo max(array(2, 4, 5)); // 5


With multiple arrays, max compares from left to right
so in our example: 2 == 2, but 4 < 5
$val = max(array(2, 4, 8), array(2, 5, 7)); // array(2, 5, 7)


This is great for determining the largest VALUE, but is there a way to find out which VARIABLE is the largest? I know traditionally you would just use a complex if-statement to find out the largest value, but as I learn more and more about PHP...it seems rather UNtraditional, as in -- there are MANY built-in functions that make it easier to do some of these common things.

The only way I can think of doing it is by using something I learned in java a long time ago, testing each variable against each other variable. This is fine for 3 objects, but what about when you have 10 or more?.
class largernumber{
  public static void main(String[] args) {
	int x=500, y=70, z=3000;
	if (x>y){
	  if (x>z){
		System.out.println("x is greater");
	  }
	  else{
		if(z>y){
		  System.out.println("z is greater"); 
		}
		else{
		  System.out.println("y is greater");
		}
	  }
	}
	else{
	  if (y>z){
		System.out.println("y is greater");
	  }
	}
  }
}


This is not very efficient.

Is This A Good Question/Topic? 0
  • +

Replies To: Determining largest VARIABLE, not VALUE

#2 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: Determining largest VARIABLE, not VALUE

Posted 17 November 2008 - 02:10 PM

You may sort one array of them then search with the other array in first one
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Determining largest VARIABLE, not VALUE

Posted 17 November 2008 - 02:22 PM

I don't know of any built-in function.
$x=5;
$y=10;
$z=2;
$a=4;
$arrayofvars= explode(":","x:y:z:a");
$myarray = explode(":",$x.":".$y.":".$z.":".$a);
$n= count($myarray);
$max =0;
for ($i=0;$i<$n; $i++)
	{
	if((int)$myarray[$i] > (int)$myarray[$max])
		{
		$max = $i;
		}
	}
		echo "<br>Largest variable is ".$arrayofvars[$max]." with a value of ".$myarray[$max];


This post has been edited by CTphpnwb: 17 November 2008 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Determining largest VARIABLE, not VALUE

Posted 17 November 2008 - 02:39 PM

CTphpnwb, you are outdoing yourself today! :^:

Thanks a ton. So basically all I need to do is replace x, y, z for names of the variables im trying to work with, and the if statement will take care of the rest because we are working with a count of the total variables!

Fantastic. I see no error checking tho, what if y=10 and z=10. y ends up being the "winner".
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Determining largest VARIABLE, not VALUE

Posted 17 November 2008 - 02:50 PM

View PostSonastylol, on 17 Nov, 2008 - 01:39 PM, said:

I see no error checking tho, what if y=10 and z=10. y ends up being the "winner".

Yes, I just let the first largest one be the winner. You could easily change it to anything you like. I just wanted an example.
Was This Post Helpful? 0
  • +
  • -

#6 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Determining largest VARIABLE, not VALUE

Posted 17 November 2008 - 02:54 PM

View PostCTphpnwb, on 17 Nov, 2008 - 01:50 PM, said:

View PostSonastylol, on 17 Nov, 2008 - 01:39 PM, said:

I see no error checking tho, what if y=10 and z=10. y ends up being the "winner".

Yes, I just let the first largest one be the winner. You could easily change it to anything you like. I just wanted an example.


Sorry, I should have been more clear. These "hits" or "counters" will be coming from a database of word matches.

If 2 categories have the same amount of matches, I need to be able to retain both of those categories so that I can display:

X had a value of 5.
Y also had a value of 5.

I know it must have something to do with keeping track of "5" as the highest value, which I believe from the above is $max, and then going back and checking to see which variables have 5 as a value, right?
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Determining largest VARIABLE, not VALUE

Posted 17 November 2008 - 05:07 PM

Ok, how's this?
<?php
$maxwords = array();
$words = array();
$wordcount = array();
$sentence = "This is and example. It is only an example.";
$words = explode(" ",$sentence);
$wordcount= array_count_values($words);
$max = 0;
foreach ($wordcount  as $key=>$value)
	{
	echo $key. " ".$value."<br>";
	if ($value >= $max)
		{
		$max = $value;
		}
	} 
echo "<br>";
foreach ($wordcount  as $key=>$value)
	{
	if ($value == $max)
		{
		$maxwords[$key]=$value;
		echo "The word '".$key."' appears ".$value." times.<br>";
		}
	}
?>

Was This Post Helpful? 0
  • +
  • -

#8 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Determining largest VARIABLE, not VALUE

Posted 17 November 2008 - 07:17 PM

Thanks CT,

got another question.


On digg, if you click one of the categories, the link is

Digg.com/pc_gaming, or Digg.com/politics, or anything similar to that.

When you go to these pages, the information shown is different. The only way I know how to do this is with get, but my pages always look like this:

Website.com/yourfile.php?variable=1&variable2=2

How do they make it so "clean"?

is there a folder for each category in the domain?

public_html
---pc_gaming
---politics

and then inside each folder is index.php that loads completely different code?

I doubt it..but im clueless.
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Determining largest VARIABLE, not VALUE

Posted 17 November 2008 - 08:19 PM

They edit their php.ini file.
http://www.askapache...and-tricks.html
Was This Post Helpful? 0
  • +
  • -

#10 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Determining largest VARIABLE, not VALUE

Posted 18 November 2008 - 08:40 AM

View PostCTphpnwb, on 17 Nov, 2008 - 09:19 PM, said:

They edit their php.ini file.

Actually, they generally edit their .htaccess file, or whatever the equivalent is for the web server in question. Mapping URLs to programs is handled on the web server level, so trying to do this through php.ini is really taking the long way around.

The typical solution is to use mod_rewrite, the Apache URL rewriting engine. It allows you to arbitrarily rewrite URLs using regular expressions. You can use this to map portions of the URL to GET variables that are them passed into your script. Another common method is to redirect all requests to non-static contents to a single PHP script which will parse the $_SERVER['REQUEST_URI'] and pass it off to the appropriate module. You can see an example of this in the ZEND Framework tutorial.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1