Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用条件从Php数组中提取_Php_Arrays_Extract - Fatal编程技术网

使用条件从Php数组中提取

使用条件从Php数组中提取,php,arrays,extract,Php,Arrays,Extract,我想根据“RouterName”从PHP数组中提取数据组。 最后,我将得到4个大阵列(ArrDeviceA、ArrDeviceB等) 我不想使用foreach和循环每一行并将其放入单独的数组中。此外,有些数组可能包含3行以上的行。行数不是恒定的 PHP中是否有查询数组的函数? php数组的原始数据如下: Array ( [0] => Array ( [RouterName] => DeviceA [Refere

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

我不想使用foreach和循环每一行并将其放入单独的数组中。此外,有些数组可能包含3行以上的行。行数不是恒定的

PHP中是否有查询数组的函数?

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
        )

)

PHP中是否有查询数组的函数?

在PHP中,您可以使用函数
In_array()
()检查是否存在某个键,以及
array_search()
(),该函数与
In_array()
函数几乎相同,但返回数组的索引值而不是布尔值

它们不会作为
mysql\u query()
函数进行查询,但您可以这样做:

function array_query($array,$what){
    if(in_array($what, $array)){
         return $array[array_search($what, $array)];
    }
    return false;
}
我希望这对你有用

编辑:我发现了一个
array\u search()
类似于多维数组(您的示例),代码在上面,您只需将
array\u search($what,$array)
函数与
递归数组搜索($array,$what)
函数交换即可:

function recursiveArraySearch($haystack, $needle, $index = null)
{
    $aIt     = new RecursiveArrayIterator($haystack);
    $it    = new RecursiveIteratorIterator($aIt);

    while($it->valid())
    {       
        if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) {
            return $aIt->key();
        }

        $it->next();
    }

    return false;
} 
我不想使用foreach和循环每一行并将其放入单独的数组中

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

$indexedByRouterName = array();
foreach ($array as $key => $value) {
    $routerName = $value['RouterName'];
    $indexedByRouterName[$routerName][] = $value;
}
如果使用[]运算符,则行数未知不是问题

absynthe中的库允许您对PHP数组执行SQL查询。我认为它还不支持GROUPBY子句,但它可能提供搜索数组的其他方法的替代方法