Jump to content

Matching one object value and pulling out array


Ninjakreborn

Recommended Posts

This might be...easier than i'm trying to make it. For some reason it's failing me.

 

Array
(
    [0] => stdClass Object
        (
            [player_id] => 11
            [orders] => 5
            [q1Points] => 110.75
            [q2Points] => 109
            [q3Points] => 0
            [q4Points] => 36
            [total_points] => 255.75
        )

    [1] => stdClass Object
        (
            [player_id] => 39
            [orders] => 2
            [q1Points] => 75.5
            [q2Points] => 100.25
            [q3Points] => 0
            [q4Points] => 45.5
            [total_points] => 221.25
        )

    [2] => stdClass Object
        (
            [player_id] => 38
            [orders] => 1
            [q1Points] => 63
            [q2Points] => 70
            [q3Points] => 0
            [q4Points] => 83.25
            [total_points] => 216.25
        )

    [3] => stdClass Object
        (
            [player_id] => 41
            [orders] => 3
            [q1Points] => 73.75
            [q2Points] => 54
            [q3Points] => 0
            [q4Points] => 71
            [total_points] => 198.75
        )

    [4] => stdClass Object
        (
            [player_id] => 23
            [orders] => 7
            [q1Points] => 88.75
            [q2Points] => 59
            [q3Points] => 0
            [q4Points] => 43.125
            [total_points] => 190.875
        )

    [5] => stdClass Object
        (
            [player_id] => 37
            [orders] => 4
            [q1Points] => 30.25
            [q2Points] => 76
            [q3Points] => 0
            [q4Points] => 55.875
            [total_points] => 162.125
        )

    [6] => stdClass Object
        (
            [player_id] => 43
            [orders] => 6
            [q1Points] => 44
            [q2Points] => 23.25
            [q3Points] => 0
            [q4Points] => 39.625
            [total_points] => 106.875
        )

    [7] => stdClass Object
        (
            [player_id] => 44
            [orders] => 8
            [q1Points] => 36.25
            [q2Points] => 22.5
            [q3Points] => 0
            [q4Points] => 32
            [total_points] => 90.75
        )

)

 

I want to find which item has the highest Q4Points value and get the player ID from it.  That should be easy, but for some reason my mind is

trying to make it complicated.

You just need a simple linear search:

 

<?php
$highestPoints = 0.0;
$highestObject;

foreach ($array as $val) {
   if ($val->Q4Points >= $highestPoints) {
      $highestObject = $val;
      $highestPoints = $val->Q4Points;
   }
}

$id = $highestObject->ID;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.