marnaels marnaels - 1 month ago 7
PHP Question

Filter array with subarray using array_filter returns empty array

I try to filter an array by

user_id
key using
array_filter
. The array has sub array as you can see bellow.

I expect to get back only the arrays with the given
user_id
. The result of my code is an empty array.

My code:

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

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


Array:

[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
)

)

Answer

array_filter itself iterates over the array. You do not need foreach loop. Just pass the actual array and then filter accordingly as below:

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