Excerpt from Php array using conditions

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

I do not want to use foreach and loop all the lines and put them in a separate array. Moreover, it is possible that some arrays may contain more than three lines. The number of rows is not constant.

Is there any function to query an array in PHP?
alt text http://img208.imageshack.us/img208/7616/18077470.jpg

The raw data of the php array is as follows:

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
source share
3 answers

foreach .

? 4 , ... .

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

, [].

+3

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), in_array(), .

mysql_query(), - :

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

,

EDIT: array_search() ( ), , array_search($what, $array) recursiveArraySearch($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;
} 
+2

There is an absinthe SQL4Array library that allows you to execute SQL queries against a PHP array. I don't believe that it still supports GROUP BY clauses, but it can provide an alternative to other array search methods

+1
source

Source: https://habr.com/ru/post/1751876/


All Articles