PHP检索最小值和最大值在二维关联数组 [英] PHP Retrieve minimum and maximum values in a 2D associative array

查看:147
本文介绍了PHP检索最小值和最大值在二维关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个格式的数组:

Array
(
    [0] => Array
        (
            [id] => 117
            [name] => Networking
            [count] => 16
        )

    [1] => Array
        (
            [id] => 188
            [name] => FTP
            [count] => 23
        )

    [2] => Array
        (
            [id] => 189
            [name] => Internet
            [count] => 48
        )

)

有检索最小和计数的最大值的好办法?我可以做到这一点使用了几个圈,但我认为有可能是一个更好的办法。

Is there a good way to retrieve the minimum and maximum values of 'count'? I could do this using a few loops but I thought there may be a better way.

推荐答案

在对比别人已经公布,您不能使用的 MIN() /的 MAX() 对于这个问题,因为这些功能不明白这是在传递的数据结构(阵列)功能。这些功能仅适用于工作标量数组元素。

In contrast to what others have posted, you cannot use the min()/max() functions for this problem as these functions do not understand the datastructure (array) which are passed in. These functions only work for scalar array elements.


BEGIN修改

之所以使用 MIN() MAX() 似乎得到正确答案的类型转换阵列整数是有关这是一个<一个href=\"http://de3.php.net/manual/en/language.types.integer.php#language.types.integer.casting\">undefined行为:

The reason why the use of min() and max() seem to yield the correct answer is related to type-casting arrays to integers which is an undefined behaviour:

转换的行为为整
  未定义的其他类型。不要
  依赖任何观察到的行为,因为它
  可以更改,恕不另行通知。

我上面关于类型转换是错误的。其实 MIN() 和<一个HREF =htt​​p://de3.php.net/manual/en/function.max.php> MAX() 不要使用数组但不在程OP需要他们的工作。当使用 MIN() 和< A HREF =htt​​p://de3.php.net/manual/en/function.max.php> MAX() 与多个阵列或数组元素的数组进行比较逐个元素从左至右:

My statement above about the type-casting was wrong. Actually min() and max() do work with arrays but not in the way the OP needs them to work. When using min() and max() with multiple arrays or an array of arrays elements are compared element by element from left to right:

$val = min(array(2, 4, 8), array(2, 5, 1)); // array(2, 4, 8)
/*
 * first element compared to first element: 2 == 2
 * second element compared to second element: 4 < 5
 * first array is considered the min and is returned
 */

翻译成OP的问题,这说明了为什么直接用 分钟() MAX() 似乎得到正确的结果。该数组的第一个元素是 ID - 值,因此的 MIN() MAX() 将首先对它们进行比较,顺便导致正确的结果,因为最低 ID 是一个与最低计数,最高 ID 是具有最高计数

Translated into the OP's problem this shows the reason why the direct use of min() and max() seems to yield the correct result. The arrays' first elements are the id-values, therefore min() and max() will compare them first, incidentally resulting in the correct result because the lowest id is the one with the lowest count and the highest id is the one with the highest count.

END修改


正确的方法是使用一个循环。

The correct way would be to use a loop.

$a = array(
        array('id' => 117, 'name' => 'Networking', 'count' => 16),
        array('id' => 188, 'name' => 'FTP', 'count' => 23),
        array('id' => 189, 'name' => 'Internet', 'count' => 48)
);
$min = PHP_INT_MAX;
$max = 0;
foreach ($a as $i) {
    $min = min($min, $i['count']);
    $max = max($max, $i['count']);
}

这篇关于PHP检索最小值和最大值在二维关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆