# Determining largest VARIABLE, not VALUE

Page 1 of 1

## 9 Replies - 2870 Views - Last Post: 18 November 2008 - 08:40 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=72312&amp;s=cd9365e3ae9357ff83729f1c2abe321b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Sonastylol

Reputation: 1
• 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

• D.I.C Regular

Reputation: 30
• 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

### #3 CTphpnwb

• D.I.C Lover

Reputation: 3837
• Posts: 13,994
• 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

### #4 Sonastylol

Reputation: 1
• 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".

### #5 CTphpnwb

• D.I.C Lover

Reputation: 3837
• Posts: 13,994
• Joined: 08-August 08

## Re: Determining largest VARIABLE, not VALUE

Posted 17 November 2008 - 02:50 PM

Sonastylol, 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.

### #6 Sonastylol

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

## Re: Determining largest VARIABLE, not VALUE

Posted 17 November 2008 - 02:54 PM

CTphpnwb, on 17 Nov, 2008 - 01:50 PM, said:

Sonastylol, 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?

### #7 CTphpnwb

• D.I.C Lover

Reputation: 3837
• Posts: 13,994
• 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>";
}
}
?>
```

### #8 Sonastylol

Reputation: 1
• 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.

### #9 CTphpnwb

• D.I.C Lover

Reputation: 3837
• Posts: 13,994
• Joined: 08-August 08

## Re: Determining largest VARIABLE, not VALUE

Posted 17 November 2008 - 08:19 PM

They edit their php.ini file.

• Resident Curmudgeon

Reputation: 463
• Posts: 820
• Joined: 17-June 08

## Re: Determining largest VARIABLE, not VALUE

Posted 18 November 2008 - 08:40 AM

CTphpnwb, 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.