Hightest Value of an Associative Array

Hightest value of an associative array

If you know your data will always be in that format, something like this should work.

function getMax( $array )
{
$max = 0;
foreach( $array as $k => $v )
{
$max = max( array( $max, $v['key1'] ) );
}
return $max;
}

Get highest value from associative array foreach

In the answer you received on the other post:

$temp = array();
foreach ($data as $item) {
foreach ($item as $key => $value) {
$temp[$key] = max(
isset($temp[$key]) ? $temp[$key] : $value,
$value);
}
}

You go through all the array of objects, updating the max value as you go through all the data elements.
That means that you will only have the maximum value (with 100% certainty) when you reach the end of that loop.


If what you want is to print a trophy icon alongside the maximum value, you need to know beforehand what is that element.

You have 2 ways to do that:

1. Use two loops

Use a first loop, without writes, just to find the maximum values.
Then use a second loop, with writes, writing the object data. And, if the maximum value gathered in the first loop equals the object data, draw the trophy icon.

Something like:

$temp = array();
foreach ($data as $item) {
foreach ($item as $key => $value) {
$temp[$key] = max(
isset($temp[$key]) ? $temp[$key] : $value,
$value);
}
}

foreach ($data as $item) {
foreach ($item as $key => $value) {
echo $user_data;
if ($user_data_sum == $temp[$key])
echo "<trophy image>";
}
}

2. Save on the object that it is the maximum value (or the trophy)

If you can edit the objects, save the maximum value on the object. That will reduce the processing time, since you'll only need a single loop instead of two as above.

Something like:

[0] => stdClass Object
(
[userid] => 1
[week] => 1
[sum] => 28
[user] => al
[is_max] => false
)
[1] => stdClass Object
(
[userid] => 1
[week] => 2
[sum] => 33
[user] => al
[is_max] => true
)

Using on the loop:

foreach ($data as $item) {
foreach ($item as $key => $value) {
echo $user_data;
if ($user_data_is_max)
echo "<trophy image>";
}
}

Or even:

[0] => stdClass Object
(
[userid] => 1
[week] => 1
[sum] => 28
[user] => al
[trophy] => ""
)
[1] => stdClass Object
(
[userid] => 1
[week] => 2
[sum] => 33
[user] => al
[trophy] => "<trophy image>"
)

Using on the loop just prints (without needing the if):

foreach ($data as $item) {
foreach ($item as $key => $value) {
echo $user_data . $user_data_trophy;
}
}

Get the id of maximum amount values in a associative array

It's simplest to just initialise a "maxkey" value with 0 and then iterate over the array, replacing the key when you find a value with a higher amount:

$maxkey = 0;
foreach ($data as $key => $value) {
if ($value['amount'] > $data[$maxkey]['amount']) {
$maxkey = $key;
}
}
print_r($maxkey);
print_r($data[$maxkey]);

Output:

2
Array
(
[id] => 189
[name] => Grapes
[amount] => 7000
)

Finding Highest Value in Associative Array

Iterate over the Array and store the max values for each key

$temp = array();
foreach ($data as $item) {
foreach ($item as $key => $value) {
$temp[$key] = max(
isset($temp[$key]) ? $temp[$key] : $value,
$value);
}
}

Php associative array sort and get key with highest length

You can use array_keys and pass a second parameter to filter the returned keys to only include the max ones. You can then find the longest key by using array_reduce and a function that checks the lengths of the strings and throws out the shortest one, like so:

$array = array(
'abc' => 10,
'def' => 8,
'fff' => 3,
'abcr' => 10,
'adsfefs' => 10
);

$keys = array_keys($array, max($array));
$longestKey = array_reduce($keys, function ($a, $b) { return strlen($a) > strlen($b) ? $a : $b; });

var_dump($longestKey);

Be aware that if there are two or more strings that are the same length, it will return the last one.

Find minimum and maximum key and value of an associative array php

The solution using array_map, array_intersect, min, max and asort functions:

$result = array_map(function($y){
$slice = array_intersect($y, [min($y), max($y)]);
asort($slice);

return $slice;
}, $year);

// the min/max items are in ascending order (minimum values, then - maximun values)
print_r($result);

The output:

Array
(
[year 2015 ] => Array
(
[Television] => 3
[printer] => 5
[personal computer] => 5
)

[year 2016 ] => Array
(
[printer] => 1
[phone] => 7
)
)

How to get maximum key/values pair array from a associative array in PHP?

You can use bellow approach for getting your result :

$array = array(
array('K1'=>'v1'),
array('K1'=>'v1','K2'=>'v2'),
array('K1'=>'v1','K2'=>'v2','K3'=>'v3')
);
$maxs = array_keys($array, max($array));
print_r($maxs);

It will provide you output as :

Array ( [0] => 2 ) 

Then you can get your key value pairs according to your requirement after passing this maxs array value like this.

$myMaxValue = $array[$maxs[0]];
print_r($myMaxValue);

This provide you below result :

Array ( [K1] => v1 [K2] => v2 [K3] => v3 ) 


Related Topics



Leave a reply



Submit