1

I have an array and would like to find the object with maximal rating? My array looks like below:

Array
(
    [0] => stdClass Object
        (
            [created_by] => 905
            [rating] => 1
        )
    [1] => stdClass Object
        (
            [created_by] => 906
            [rating] => 2
        )
)

Thanks!

I have tried something like that:

static function maxValueInArray($array, $keyToSearch) {
        $currentMax = NULL;
        foreach($array as $arr) {
            foreach($arr as $key => $value) {
                if ($key == $keyToSearch && ($value >= $currentMax)) {
                    $currentMax = $value;
                }
            }
        }

        return $currentMax;
    }
6
  • 2
    Can you share what you have tried? Or did you want someone to just write the code for you? Is a 1 rating better than a 2? By 'max' do you mean highest or lowest number? Commented Nov 4, 2014 at 13:18
  • Highest number I mean. Commented Nov 4, 2014 at 13:20
  • Looks like you need something like php.net/array_column Commented Nov 4, 2014 at 13:29
  • @JayBlanchard You can't search nested Objects with max() or array_search(). That's no duplicate of the linked question. Commented Nov 4, 2014 at 13:56
  • possible duplicate of stackoverflow.com/questions/2189479/… Better @kaiser? Commented Nov 4, 2014 at 13:57

4 Answers 4

2

Since your array is not sorted (I assume), you will need to read all values.

$maxobject = null;
foreach($array as $row)
  if(is_null($maxobject) || $maxobject->rating<$row->rating)
    $maxobject=$row;
Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

<?php 

    ...

    $rating = -1;
    $max_object = (object) null;
    foreach ($your_array as $obj) {

        if($obj->rating > $rating){
            $rating = $value->rating;
            $max_object = $obj;
        }

    }

    print_r($max_object); // gives : stdClass Object ( [created_by] => 906 [rating] => 2 )

    ...

?>

Comments

0

Should you need to access other objects in the array based on their rating (eg to show highest to lowest), you can sort the array:

usort($array, function($a, $b){
    if ($a->rating == $b->rating) {
        return 0;
    }
    return ($a->rating > $b->rating) ? -1 : 1;
});

To get the highest rated, select the 1st element:

$highest_rated = reset($array);
echo 'highest rating: ' . $highest_rated->rating;

If you only need to select the highest rated element, simply iterating the array once as per David's answer, will be more efficient

Comments

0

That's a pretty perfect example on when to use as Heap. You want to extend the default SPL \SplMaxHeap to know where to look for a comparison value - in your case that's the rating property.

Custom Max Heap

The benefit of using a heap is that PHP doesn't need to make an internal copy (as with normal arrays), which reduces memory usage quite a bit. It also is more readable and can be adjusted much easier.

class MaxRatingHeap extends \SplMaxHeap
{
    public function compare( $old, $new )
    {
        return $old->rating - $new->rating;
    }
}

Test

Some dummy data:

$objA = new \stdClass;
$objA->created_by = 103;
$objA->rating = 3;
$objB = new \stdClass;
$objB->created_by = 102;
$objB->rating = 2;
$objC = new \stdClass;
$objC->created_by = 100;
$objC->rating = 5;
$objD = new \stdClass;
$objD->created_by = 101;
$objD->rating = 1;

Run the test:

$it = new \MaxRatingHeap;
$it->insert( $objA );
$it->insert( $objB );
$it->insert( $objC );
$it->insert( $objD );

$it->rewind();
if ( ! $it->isEmpty() )
{
    printf( 'Our top rating is %s', $it->top()->rating );
    // Uncomment for proof that it is ordered by the `rating` property
    /*while ( $it->valid() )
    {
        var_dump( $it->current() );
        $it->next();
    }*/
}

Output:

Our top rating is 5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.