views:

47

answers:

3

I would like to extract the data groups from the PHP arrays depending on the "RouterName". So, in the end, I will get 4 Big Arrays (ArrDeviceA, ArrDeviceB, etc.)

I don't want to use foreach and loop every rows and put into the separate array. Moreover, there might be the chance that some arrays might contain more than 3 rows. The number of rows are not constant.

Is there any function to query the array in PHP?
alt text

The raw data of php array are as follow:

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
        )

)
+2  A: 

Is there any function to query the array in PHP?

In PHP you can use the function in_array() ( http://www.php.net/manual/en/function.in-array.php ) to check if some key exists, and array_search() ( http://www.php.net/manual/en/function.array-search.php ) that makes almost the same of in_array() function, but returns the index value of the array instead of a boolean value.

They won't query as mysql_query() function, but you can make something like this:

function array_query($array,$what){
    if(in_array($what, $array)){
         return $array[array_search($what, $array)];
    }
    return false;
}

I Hope this work for you

EDIT: I've found an array_search() like for multidimensional arrays (your example), the code is above, you only have to trade on my function the array_search($what, $array) function with recursiveArraySearch($array,$what) function:

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;
} 
CuSS
+2  A: 

I don't want to use foreach and loop every rows and put into the separate array.

Why ? That's 4 lines of crystal clear code, and you could even save a line... Stay simple.

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

Having an unknown number of lines is not a problem if you use the [] operator.

mexique1
A: 

There's the SQL4Array library from absynthe that allows you to execute SQL queries against a PHP array. I don't believe it yet supports GROUP BY clauses, but it may provide an alternative to other methods of searching arrays

Mark Baker