Find highest value in multidimensional array
Since PHP 5.5 you can use array_column to get an array of values for specific key, and max it.
max(array_column($array, 'Total'))
How to find the highest value in a multidimensional array?
You can try using recursive function
<?php
$ar3=array(123, array(12, 665, array(77, 255, 98, 56), 8), 1155, 676);
function highestValue($ar3) {
foreach($ar3 as $key => $value) {
if (is_array($value)) {
$ar3[$key] = highestValue($value);
}
}
return max($ar3);
}
echo highestValue($ar3); //1155
Finding the Max value in a two dimensional Array
Max of max numbers (map(max, numbers)
yields 1, 2, 2, 3, 4):
>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]
>>> map(max, numbers)
<map object at 0x0000018E8FA237F0>
>>> list(map(max, numbers)) # max numbers from each sublist
[1, 2, 2, 3, 4]
>>> max(map(max, numbers)) # max of those max-numbers
4
find max() of specific multidimensional array value in php
Think array_reduce
if you want to compute some single value iteratively over an array:
$max = array_reduce($array, function($a, $b) {
return $a > $b['time'] ? $a : $b['time'];
} );
Or you could make use of a utility function like:
function array_col(array $a, $x)
{
return array_map(function($a) use ($x) { return $a[$x]; }, $a);
}
which would be used like:
$max = max(array_col($array, 'time'));
It's more general purpose and could be used for other similar things.
Get the highest value from multidimensional array based on mentioned array in php
Doing this in PHP is simple enough, you could just loop through the categories and check first that the category_id is in the array of category id's you are interested in, then compare the final_price against the current maximum...
$max = ["final_price" => PHP_INT_MIN];
foreach ( $categories as $category ) {
if ( in_array($category["category_id"], $catids)
&& $category["final_price"] > $max["final_price"] ) {
$max = $category;
}
}
print_r($max);
which with the test data gives...
Array
(
[category_id] => 240
[final_price] => 3500
)
Get highest value from multidimensional array
Maybe something like this:
$arrOrg = [2, 3, [5, 7, 1], 100, [6, 9, [14, 95]], 78];
$json = json_encode($arrOrg);
$json = str_replace(array('[', ']'), "", $json);
$arr = explode(",", $json);
echo $maximum = max($arr);
EDIT after Vahe Galstyan comment
$arrOrg = array(
2,
3,
array(5, 7, 1),
100,
array(
6,
9,
array(14, 95)
),
78
);
$json = json_encode($arrOrg);
$json = str_replace(array('[', ']'), "", $json);
$arr = explode(",", $json);
echo $maximum = max($arr);
Retrieve key from max value in multidimensional associativ array
Working off of your response, I would suggest these small tweaks:
<?php
$resultCache[129] = [
'total' => 1000,
'free_from' => "2000",
'addinupshippingcosts' => "0",
'articles' => [
['shipping_costs_total' => 11],
['shipping_costs_total' => 23],
['shipping_costs_total' => 25], //<= i want the key of this array entry
['shipping_costs_total' => 12],
]
];
foreach($resultCache as $r) {
$col = array_column($r['articles'], 'shipping_costs_total');
$highest = array_keys($col, max($col));
$highest = $highest[0] ?: false;
echo $highest;
}
Hope this helps,
Find highest value in multidimensional array and keep highest
You already done most of the job by arranging the test elements per user. What remains is to filter this list.
Consider you have this list of "test-elements": (as John Doe
in your example)
$a = array("quiz" => "Foo Test 1", "score" => "90");
$b = array("quiz" => "Foo Test 1", "score" => "100");
$c = array("quiz" => "Foo Test 2", "score" => "75");
$d = array("quiz" => "Foo Test 2", "score" => "75");
$quizes = array($a, $b, $c, $d);
You can narrow it by:
$res = array();
foreach($quizes as $quize) {
if (!isset($res[$quize["quiz"]]) || ($res[$quize["quiz"]]["score"] < $quize["score"])) // is first test or this test higher then the previous
$res[$quize["quiz"]] = $quize;
}
$res = array_values($res); // because all you want are the quiz elements
Now $res
will output:
Array
(
[0] => Array
(
[quiz] => Foo Test 1
[score] => 100
)
[1] => Array
(
[quiz] => Foo Test 2
[score] => 75
)
)
Hope that helps!
Edited
Define the following function:
function filterQuizesByScore($quizes) {
$res = array();
foreach($quizes as $quize) {
if (!isset($res[$quize["quiz"]]) || ($res[$quize["quiz"]]["score"] < $quize["score"]))
$res[$quize["quiz"]] = $quize;
}
return array_values($res);
}
Use it in student_scores
function. After foreach ( $quizzes as $x )
scope add new loop as:
foreach($out as &$elem) {
$elem["quizes"] = filterQuizesByScore($elem["quizes"]);
}
Related Topics
How to Use Basic Authorization in PHP Curl
Laravel View Not Found Exception
Why Is PHP Session_Destroy() Not Working
Change Product Prices Via a Hook in Woocommerce 3+
Why Is Json_Encode Adding Backslashes
How to Get the User'S Local Time Instead of the Server'S Time
Replace a Whole Line Where a Particular Word Is Found in a Text File
Header Only Retrieval in PHP Via Curl
Check If a User/Guest Has Purchased Specific Products in Woocommerce
Formatting Datetime Object, Respecting Locale::Getdefault()
How to Properly Escape HTML Form Input Default Values in PHP
Woocommerce: Which Hook to Replace Deprecated "Woocommerce_Add_Order_Item_Meta"
The MySQLi Extension Is Missing. Please Check Your PHP Configuration
How to Access a Property With an Invalid Name
Best Way to Do Multiple Constructors in PHP
How to Make a Simple Crawler in PHP