检查数组中键值存在多少时间?

时间:2015-05-21 09:25:45

标签: php arrays

我想检查数组中的key=>value,存在多少次。 我在这里举个例子。检查Array [DriverLocation] [driver_id] = 83是否在以下数组中出现3次。 这是我要做的,如果存在相同的键值,则删除此内部数组并继续获取兄弟数据。 例如:

Array
(
    [0] => Array
        (
            [DriverLocation] => Array
                (
                    [id] => 15
                    [dispensary_id] => 1
                    [driver_id] => 85
                    [zip_code_id] => 43
                    [created] => 2015-05-20 12:25:34
                )

            [ZipCode] => Array
                (
                    [id] => 43
                    [province_id] => 3846
                    [city] => Rohtak
                    [zip_code] => 15478
                    [status] => active
                )

            [UserProfile] => Array
                (
                    [first_name] => Arman
                    [last_name] => Kumar
                )

        )

    [1] => Array
        (
            [DriverLocation] => Array
                (
                    [id] => 19
                    [dispensary_id] => 1
                    [driver_id] => 43
                    [zip_code_id] => 42
                    [created] => 2015-05-20 12:37:12
                )

            [ZipCode] => Array
                (
                    [id] => 42
                    [province_id] => 3846
                    [city] => Rohtak
                    [zip_code] => 30215
                    [status] => active
                )

            [UserProfile] => Array
                (
                    [first_name] => Pawan
                    [last_name] => Kumar
                )

        )

    [2] => Array
        (
            [DriverLocation] => Array
                (
                    [id] => 20
                    [dispensary_id] => 1
                    [driver_id] => 83
                    [zip_code_id] => 42
                    [created] => 2015-05-20 12:37:28
                )

            [ZipCode] => Array
                (
                    [id] => 42
                    [province_id] => 3846
                    [city] => Rohtak
                    [zip_code] => 30215
                    [status] => active
                )

            [UserProfile] => Array
                (
                    [first_name] => Ramesh
                    [last_name] => Saini
                )

        )

    [3] => Array
        (
            [DriverLocation] => Array
                (
                    [id] => 26
                    [dispensary_id] => 1
                    [driver_id] => 83
                    [zip_code_id] => 43
                    [created] => 2015-05-20 12:43:59
                )

            [ZipCode] => Array
                (
                    [id] => 43
                    [province_id] => 3846
                    [city] => Rohtak
                    [zip_code] => 15478
                    [status] => active
                )

            [UserProfile] => Array
                (
                    [first_name] => Ramesh
                    [last_name] => Saini
                )

        )

    [4] => Array
        (
            [DriverLocation] => Array
                (
                    [id] => 41
                    [dispensary_id] => 1
                    [driver_id] => 83
                    [zip_code_id] => 6
                    [created] => 2015-05-21 05:23:53
                )

            [ZipCode] => Array
                (
                    [id] => 6
                    [province_id] => 3846
                    [city] => Whittier
                    [zip_code] => 90607
                    [status] => active
                )

            [UserProfile] => Array
                (
                    [first_name] => Ramesh
                    [last_name] => Saini
                )

        )

)

4 个答案:

答案 0 :(得分:3)

你可以从PHP5.5使用array_column函数的2倍。此函数将该键的所有值放在一个数组中。

$driver_ids = array_column(array_column($array, 'DriverLocation'), 'driver_id');

print_r(array_count_values($driver_ids));

Result:
Array ( [85] => 1 [43] => 1 [83] => 3 ) 

5.5之前的PHP版本:

$driver_ids = array_map(function ($ar) {return $ar['DriverLocation']['driver_id'];}, $array);

print_r(array_count_values($driver_ids));

答案 1 :(得分:1)

使用array_key_exists()功能。

答案 2 :(得分:1)

您可以使用array_filtercount

echo count(array_filter($array,function($element){
    if(isset($element['DriverLocation']['driver_id'])) {
        return $element['DriverLocation']['driver_id'] === 83;
    }
}));

答案 3 :(得分:1)

作为替代方案,如果您只想要83的第一个条目作为驱动程序ID,您可以先收集所有值,将它们放在另一个容器中,只将第一个放在那里并将其用作关键字。

只需添加一个条件,如果它还没有,请将其放入其中,如果它已经没有包含,那么最后你只剩下每个驱动程序的第一个唯一条目。

$drivers = array();
foreach($data as $k => $values) {
    if(!isset($drivers[$values['DriverLocation']['driver_id']])) {
        $drivers[$values['DriverLocation']['driver_id']] = $values;
    }
}

// $drivers = array_values($drivers); // optional array re-index

Sample Output