使用array_filter使用子数组过滤数组返回空数组

时间:2016-11-02 13:01:57

标签: php arrays

我尝试使用user_idarray_filter键过滤数组。如下所示,数组有子数组。

我希望只返回给定user_id的数组。我的代码的结果是一个空数组。

我的代码:

foreach ($this->analyticsData['data'] as $data) {

    $filteredArray = array_filter($data['author'], function ($data) {
        return $data == '17';
    });
}

阵列:

 [data] => Array
        (
            [0] => Array
                (
                    [pagePath] => /en/thing/guiness-world-record-in-limassol0
                    [pageTitle] => Guiness World Record in Limassol - Chain of 280 divers in Limassol broke the Guinness record...
                    [wholeProfit] => 8.3359394004686
                    [uniquePageView] => Array
                        (
                            [qty] => 440
                            [percentage] => 8.6477987421384
                            [profit] => 4.3238993710692
                        )

                    [entrance] => Array
                        (
                            [qty] => 410
                            [percentage] => 14.30565247732
                            [profit] => 3.5764131193301
                        )

                    [avgTime] => Array
                        (
                            [qty] => 00:04:23
                            [sec] => 263.58
                            [percentage] => 0.46295979163121
                            [profit] => 0.046295979163121
                        )

                    [bounceRate] => Array
                        (
                            [in] => 87.07317073170732
                            [out] => 12.93
                            [percentage] => 0.03861857215819
                            [profit] => 0.003861857215819
                        )

                    [pageView] => Array
                        (
                            [qty] => 521
                            [percentage] => 7.7093814738088
                            [profit] => 0.38546907369044
                        )

                    [author] => Array
                        (
                            [user_id] => 17
                            [name] => Giannis  Papanastasiou
                        )

                )

            [1] => Array
                (
                    [pagePath] => /el/thing/feast-of-the-harvest
                    [pageTitle] => Γιορτή του Τρύγου - Απόσταξη ζιβανίας και επίδειξη παλουζέ
                    [wholeProfit] => 4.1913500922592
                    [uniquePageView] => Array
                        (
                            [qty] => 218
                            [percentage] => 4.2845911949686
                            [profit] => 2.1422955974843
                        )

                    [entrance] => Array
                        (
                            [qty] => 209
                            [percentage] => 7.2923935799023
                            [profit] => 1.8230983949756
                        )

                    [avgTime] => Array
                        (
                            [qty] => 00:03:05
                            [sec] => 185.46
                            [percentage] => 0.32574748826134
                            [profit] => 0.032574748826134
                        )

                    [bounceRate] => Array
                        (
                            [in] => 84.21052631578947
                            [out] => 15.79
                            [percentage] => 0.047160653857526
                            [profit] => 0.0047160653857526
                        )

                    [pageView] => Array
                        (
                            [qty] => 255
                            [percentage] => 3.773305711749
                            [profit] => 0.18866528558745
                        )

                    [author] => Array
                        (
                            [user_id] => 12
                            [name] => Christina Nicolaou
                        )

                )

            [2] => Array
                (
                    [pagePath] => /el/thing/halloween-at-cyherbia
                    [pageTitle] => Halloween στον βοτανικό πάρκο Cyherbia - Παιχνίδια εμπνευσμένα απο παραμύθια
                    [wholeProfit] => 3.307363812081
                    [uniquePageView] => Array
                        (
                            [qty] => 174
                            [percentage] => 3.4198113207547
                            [profit] => 1.7099056603774
                        )

                    [entrance] => Array
                        (
                            [qty] => 161
                            [percentage] => 5.6175854849965
                            [profit] => 1.4043963712491
                        )

                    [avgTime] => Array
                        (
                            [qty] => 00:03:21
                            [sec] => 201.89
                            [percentage] => 0.3546056314304
                            [profit] => 0.03546056314304
                        )

                    [bounceRate] => Array
                        (
                            [in] => 87.5776397515528
                            [out] => 12.42
                            [percentage] => 0.037095333813203
                            [profit] => 0.0037095333813203
                        )

                    [pageView] => Array
                        (
                            [qty] => 208
                            [percentage] => 3.0778336786031
                            [profit] => 0.15389168393016
                        )

                    [author] => Array
                        (
                            [user_id] => 12
                            [name] => Christina Nicolaou
                        )

                )

            [3] => Array
                (
                    [pagePath] => /en/thing/zivania
                    [pageTitle] => Zivania - Distilled Grape Alcohol
                    [wholeProfit] => 2.6858766693942
                    [uniquePageView] => Array
                        (
                            [qty] => 153
                            [percentage] => 3.0070754716981
                            [profit] => 1.5035377358491
                        )

                    [entrance] => Array
                        (
                            [qty] => 112
                            [percentage] => 3.9078855547802
                            [profit] => 0.97697138869505
                        )

                    [avgTime] => Array
                        (
                            [qty] => 00:03:54
                            [sec] => 234.40
                            [percentage] => 0.41170716730539
                            [profit] => 0.041170716730539
                        )

                    [bounceRate] => Array
                        (
                            [in] => 80.35714285714286
                            [out] => 19.64
                            [percentage] => 0.058659609991249
                            [profit] => 0.0058659609991249
                        )

                    [pageView] => Array
                        (
                            [qty] => 214
                            [percentage] => 3.166617342409
                            [profit] => 0.15833086712045
                        )

                    [author] => Array
                        (
                            [user_id] => 19
                            [name] => Bill Warry
                        )

                )

1 个答案:

答案 0 :(得分:0)

array_filter本身遍历数组。您不需要foreach循环。只需传递实际数组,然后进行相应过滤,如下所示:

$filteredArray = array_filter($this->analyticsData['data'], function ($data) {
    return $data['author']['user_id'] == '17';
});