php wizards i have a question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrthumbs
    salad tossing sig guy
    • Apr 2002
    • 11702

    #1

    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
    Confirmed User
    • Jun 2004
    • 6890

    #2
    I don't really use much OOP with PHP.....but if you used an array instead, you could just use the function array_sum()
    Skype variuscr - Email varius AT gmail

    Comment

    • mrthumbs
      salad tossing sig guy
      • Apr 2002
      • 11702

      #3
      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....

      Comment

      • Varius
        Confirmed User
        • Jun 2004
        • 6890

        #4
        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.
        Skype variuscr - Email varius AT gmail

        Comment

        • mrthumbs
          salad tossing sig guy
          • Apr 2002
          • 11702

          #5
          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!

          Comment

          • nakki
            Confirmed User
            • May 2001
            • 137

            #6
            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. :]
            - adult blog aggregator - sign up here
            NastyLittle.com - trade with an established adult blog

            Comment

            Working...