`Hi,
For example I’ve such array:
array:3 [
1 => array:1 [
"Home" => array:3 [
"Weight (kg)" => 4.0
"Length (cm)" => null
"Width (cm)" => null
]
]
2 => array:1 [
"Home" => array:3 [
"Weight (kg)" => 3.0
"Length (cm)" => 3.0
"Width (cm)" => null
]
]
3 => array:1 [
"Home" => array:3 [
"Weight (kg)" => 2.0
"Length (cm)" => 2.0
"Width (cm)" => null
]
]
]
Need to find a way to get biggest value & that’s not a problem, I could find it easily, but how to find biggest value & return i.e. key number 1
, cause it has the greatest value from all others.
Must be checked with locations i.e. Home
, could be an array, need to find in group all maxes & return its parent id to mark as standard elements.
Someone could help with that?
Thanks anyway
I’ve found max value by method:
$max = null;
array_walk_recursive($values, function ($value) use (&$max) {
if ($max === null || $value > $max) $max = $value;
});
return $max;
But can’t find a good way to return this element or it’s key:
1 => array:1 [
"Home" => array:3 [
"Weight (kg)" => 4.0
"Length (cm)" => null
"Width (cm)" => null
]
]
2
Answers
You could do it this way:
Output:
And if you want whole element:
Output:
The lambda you can hand over to the "array_walk" functions can accept two parameters, key and value. That should be the missing link you are looking for.
I slightly modified your approach that way:
The output obviously is:
An extended version of above code that finds and reports the maximum value for each of the given dimensions separately:
The output is: