GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   php wizards i have a question (https://gfy.com/showthread.php?t=528489)

mrthumbs 10-15-2005 02:13 PM

php wizards i have a question
 
Imagine i have and object with 50 values:

$my_obect->value_1
$my_obect->value_2
$my_obect->value_3
etc etc..

I want to add all values:
$total = $my_object->value_1 + $my_object->value_2 etc etc..

Is there a faster way to do it?

Replacing the number for a var ($my_object->value_$i or {$i} etc) in a while loop doesnt work as it would with normal vars..

Varius 10-15-2005 02:19 PM

I don't really use much OOP with PHP.....but if you used an array instead, you could just use the function array_sum()

mrthumbs 10-15-2005 02:24 PM

Quote:

Originally Posted by Varius
I don't really use much OOP with PHP.....but if you used an array instead, you could just use the function array_sum()

What i really need to check if value_1 is the highest value of the 50..

so value_1 > then v2,v3,v4,v5,v6 etc..

Thats why i need a way to loop through all these vars without having
50 lines of text..

I could move the data to an array and loop through there but i still need
to compare the vars individually..

Hmm.. i could load it into an array.. do a sort.. and see if the highest number
equals value_1 AND is unique in the array....

Varius 10-15-2005 02:30 PM

Quote:

Originally Posted by mrthumbs
What i really need to check if value_1 is the highest value of the 50..

so value_1 > then v2,v3,v4,v5,v6 etc..

Thats why i need a way to loop through all these vars without having
50 lines of text..

I could move the data to an array and loop through there but i still need
to compare the vars individually..

Hmm.. i could load it into an array.. do a sort.. and see if the highest number
equals value_1 AND is unique in the array....

To do it with a loop, you could do something like:
PHP Code:

$high_value $array[0];
$high_key 0;
foreach (
$array as $key=>$value) {
    if (
$value $high_value) { 
        
$high_value $value;
        
$high_key $key;
    }


However since their are only 50 items, yes sort might be faster.

mrthumbs 10-15-2005 02:37 PM

Quote:

Originally Posted by Varius
To do it with a loop, you could do something like:
PHP Code:

$high_value $array[0];
$high_key 0;
foreach (
$array as $key=>$value) {
    if (
$value $high_value) { 
        
$high_value $value;
        
$high_key $key;
    }


However since their are only 50 items, yes sort might be faster.

im going to try that yes.. im on it.. thankie!

nakki 10-15-2005 03:35 PM

Hey, this is how I'd do it:

Code:

/*
 * Gets the name of the member variable with the highest value
 *
 * Returns the name of the variable or false on failure
 */
function getHighestVariableName($obj) {
        if (!is_object($obj)) return false;
       
        // get all the variables of the class
        $values = get_object_vars($obj);
               
        $field = false;
        $highest = null;
       
        // loop through the variables, try to find the highest one
        foreach ($values as $key => $value) {
                if ($value > $highest) {
                        $highest = $value;
                        $field = $key;
                }
        }
       
        return $field;
}

Of course, this won't work if you only want to count certain member variables, not all of them. But I'm sure it could be modified to do that too. :]


All times are GMT -7. The time now is 07:11 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123