简体   繁体   English

在数组中找到最大值和父值

[英]Find the maximum and parent value in an array

I searched many thread but i can't find this solution I have this Array 我搜索了很多线程,但是找不到这个解决方案,我有这个数组

Array 
( [0] => [1] => Array ( [0] => 2019-01-11T23:30:00CET [1] => -12.6 ) [2] => [3] => Array ( [0] => 2019-01-11T23:20:00CET [1] => -12.5 ) [4] => [5] => Array ( [0] => 2019-01-11T23:10:00CET [1] => -12.6 ) [10] => [11] => Array ( [0] => 2019-01-11T22:40:00CET [1] => -12.4 ) 

I found the path to have the maximum or minimum value ( Column [1] ) from this Array but i need to find the relative Parent 我发现此数组具有最大值或最小值(列[1]),但我需要找到相对的Parent

(example the minimum -12.6 is in the [1][0] as 2019-01-11T22:20:00CET) of this two values that are show in the first column ( Column[0] ) (例如,最低-12.6在[1] [0]中作为2019-01-11T22:20:00CET)在第一列(Column [0])中显示的这两个值

Thanks 谢谢

If you use array_column() to extract the second column of your data, then you can use min() or max() with that array to pick which one you want. 如果使用array_column()提取数据的第二列,则可以对该数组使用min()max()来选择所需的数据。 This code then extracts the ones that match using a foreach() and if to check if it matches (not exactly sure what you want as output, but this should help)... 此代码然后提取匹配所使用的那些foreach()if来检查它是否匹配(不完全确定要作为输出什么,但这应该帮助)...

$source = [["2019-01-11T23:30:00CET", -12.6],
    ["2019-01-11T23:20:00CET", -12.5],
    ["2019-01-11T23:10:00CET", -12.6]
];

$extract = min(array_column($source, 1));  // or use max()
$output = [];
foreach ($source as $key => $element) {
    if ( $element[1] == $extract )  {
        // Matches, so add to output
        $output[$key] = $element[0];
    }
}
print_r($output);

will give 会给

Array
(
    [0] => 2019-01-11T23:30:00CET
    [2] => 2019-01-11T23:10:00CET
)

You could use array_filter() to extract the matching rows, but a foreach() is enough for a straightforward thing like this (IMHO). 您可以使用array_filter()提取匹配的行,但是foreach()足以满足此类简单要求(IMHO)。

If there is a possibility of blank values or strings in the value column, this may confuse the min() as it will consider the values and compare them as strings, to ensure they are all compared as numbers you can add... 如果value列中可能有空白值或字符串,则这可能会使min()感到困惑,因为它将考虑这些值并将它们作为字符串进行比较,以确保将它们全部作为数字进行比较,您可以添加...

$values = array_map("floatval", array_column($source, 1));
$extract = min($values);  // or use max()

The array_map("floatval",... goes through the list and converts them all to float values. array_map("floatval",...浏览列表,并将它们全部转换为浮点值。

Also, here's a generalized algorithm-sketch for "finding the max in some array", expressed as pseudo-code: 另外,这是用于“在某个数组中找到最大值”的通用算法草图,表示为伪代码:

  • "Leave quietly" if the array is empty, or throw an exception. 如果数组为空,则“安静地离开”,否则抛出异常。
  • Otherwise, assume that the first element in the array is the biggest one. 否则,假设数组中的第一个元素最大的元素。
  • Now, loop through the remaining elements, testing if each one is, in fact, bigger than the "biggest one" that you have so far. 现在,遍历其余元素,测试每个元素实际上是否大于您到目前为止所拥有的“最大元素”。 If so, select it as the "biggest." 如果是这样,请将其选择为“最大”。
  • When the loop is finished, return your answer. 循环结束后,返回您的答案。

Now – this is what a geek would call "an O(n) algorithm," which is to say that its execution-time will be "on the order of" the number of elements in the array. 现在,这就是极客所称的“ O(n)算法”,也就是说,其执行时间将“约为”数组中元素的数量。 Well, if this is a "one-off" requirement, that's fine. 好吧,如果这是“一次性”要求,那就很好。 Whereas if what you actually want to do is to get "more than one" max-element, sorting the array (once, then holding on to the sorted result ...) becomes significantly better, because the sort is going to be O(log(n)) ... "on the order of some logarithm of the number of elements," ... and the subsequent cost of "popping off" elements from that sorted array becomes non-existent. 而如果您实际想要做的是获得“多个” max元素,则对数组进行排序(一次,然后保留排序的结果...)会变得明显更好,因为排序将是O( log(n)) ...“以元素数量的一些对数的顺序”,以及随后从该排序数组中“弹出”元素的成本不再存在。

There are other ways to do it, of course – trees and such - but I've already blathered-on too long here. 当然,还有其他方法可以做到这一点-树木之类-但我在这里已经太迟了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM