2

I have array

Array (
    [1] => Array (
        [message_id] => 1,
        [points] => 3,
    )
    [2] => Array(
        [message_id] => 2,
        [points] => 2,
    )
    [3] => Array(
        [message_id] => 3,
        [points] => 2,
    )
)

and i need to get message_id value from array where points is most highest.

Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45

2 Answers2

0

You can loop in your array and check the points to find the highest one

$points = 0;
foreach($array as $val) {
    if($val['points'] > $points) {
        $points = $val['points'];
        $message_id = $val['message_id'];
    }
}

echo $message_id;
//prints 1

Live sample

Fabio
  • 23,183
  • 12
  • 55
  • 64
0

You could do it like this:

$array = array(
    1 => array(
        'message_id' => 1,
        'points' => 3,
    ),
    2 => array(
        'message_id' => 2,
        'points' => 2,
    ),
    3 => array(
        'message_id' => 3,
        'points' => 2,
    ),
);

$highest = 0;

foreach ($array as $key => $arr) {
    if($arr['message_id'] > $highest) {
        $highest = $arr['message_id'];
    }
}

echo "Highest: " . $highest;

The output for the $highest number would be: 3.

See working example.

halfer
  • 19,824
  • 17
  • 99
  • 186
Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45