使用条件从Php数组中提取

时间:2010-06-26 11:02:40

标签: php arrays extract

我想根据“RouterName”从PHP数组中提取数据组。 所以,最后,我将得到4个大阵列(ArrDeviceA,ArrDeviceB等)

我不想使用foreach并循环每一行并放入单独的数组中。此外,某些数组可能包含超过3行的可能性。行数不是常数。

是否有任何函数可以在PHP中查询数组? alt text http://img208.imageshack.us/img208/7616/18077470.jpg

php数组的原始数据如下:

Array
(
    [0] => Array
        (
            [RouterName] => DeviceA
            [Reference] => R2a
            [AverageRSSI] => -36.00
            [AverageQuality] => 63.00
            [Date_Time] => 12-June-2010
        )

    [1] => Array
        (
            [RouterName] => DeviceA
            [Reference] => R2a
            [AverageRSSI] => -51.03
            [AverageQuality] => 47.97
            [Date_Time] => 11-June-2010
        )

    [2] => Array
        (
            [RouterName] => DeviceA
            [Reference] => R2a
            [AverageRSSI] => -53.63
            [AverageQuality] => 45.37
            [Date_Time] => 10-June-2010
        )

    [3] => Array
        (
            [RouterName] => DeviceB
            [Reference] => R2
            [AverageRSSI] => -38.19
            [AverageQuality] => 60.81
            [Date_Time] => 12-June-2010
        )

    [4] => Array
        (
            [RouterName] => DeviceB
            [Reference] => R2
            [AverageRSSI] => -38.64
            [AverageQuality] => 60.36
            [Date_Time] => 11-June-2010
        )

    [5] => Array
        (
            [RouterName] => DeviceB
            [Reference] => R2
            [AverageRSSI] => -38.67
            [AverageQuality] => 60.33
            [Date_Time] => 10-June-2010
        )

    [6] => Array
        (
            [RouterName] => DeviceC
            [Reference] => SCN1010
            [AverageRSSI] => -69.12   
            [AverageQuality] => 29.88
            [Date_Time] => 12-June-2010
        )

    [7] => Array
        (
            [RouterName] => DeviceC
            [Reference] => SCN1010
            [AverageRSSI] => -70.99
            [AverageQuality] => 28.01
            [Date_Time] => 11-June-2010
        )

    [8] => Array
        (
            [RouterName] => DeviceC
            [Reference] => SCN1010
            [AverageRSSI] => -71.52
            [AverageQuality] => 27.48
            [Date_Time] => 10-June-2010
        )

    [9] => Array
        (
            [RouterName] => DeviceD
            [Reference] => SCN1020
            [AverageRSSI] => -62.48
            [AverageQuality] => 36.52
            [Date_Time] => 12-June-2010
        )

    [10] => Array
        (
            [RouterName] => DeviceD
            [Reference] => SCN1020
            [AverageRSSI] => -34.60
            [AverageQuality] => 64.40
            [Date_Time] => 11-June-2010
        )

    [11] => Array
        (
            [RouterName] => DeviceD
            [Reference] => SCN1020
            [AverageRSSI] => 0.00
            [AverageQuality] => 99.00
            [Date_Time] => 10-June-2010
        )

)

3 个答案:

答案 0 :(得分:3)

  

我不想使用foreach并循环每一行并放入单独的数组中。

为什么?这是4行清晰的代码,你甚至可以保存一条线...保持简单。

$indexedByRouterName = array();
foreach ($array as $key => $value) {
    $routerName = $value['RouterName'];
    $indexedByRouterName[$routerName][] = $value;
}

如果使用[]运算符,则具有未知数量的行不是问题。

答案 1 :(得分:2)

Is there any function to query the array in PHP?

在PHP中,您可以使用函数in_array()http://www.php.net/manual/en/function.in-array.php)检查某个密钥是否存在,array_search()http://www.php.net/manual/en/function.array-search.php)与{{{ 1}} function,但返回数组的索引值而不是布尔值。

他们不会查询in_array()函数,但您可以这样做:

mysql_query()

我希望这对你有用

编辑:我发现function array_query($array,$what){ if(in_array($what, $array)){ return $array[array_search($what, $array)]; } return false; } 类似于多维数组(您的示例),代码在上面,您只需要使用array_search()在我的函数上交换array_search($what, $array)函数功能:

recursiveArraySearch($array,$what)

答案 2 :(得分:1)

来自absynthe的SQL4Array库允许您对PHP数组执行SQL查询。我不相信它支持GROUP BY子句,但它可以提供其他搜索数组方法的替代方法