Php 使用数组的子数组过滤器数组\u过滤器返回空数组
我尝试使用Php 使用数组的子数组过滤器数组\u过滤器返回空数组,php,arrays,Php,Arrays,我尝试使用array\u filter按user\u id键过滤数组。如下图所示,该数组具有子数组 我希望只返回具有给定用户id的数组。我的代码的结果是一个空数组 我的代码: foreach ($this->analyticsData['data'] as $data) { $filteredArray = array_filter($data['author'], function ($data) { return $data == '17'; });
array\u filter
按user\u id
键过滤数组。如下图所示,该数组具有子数组
我希望只返回具有给定用户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
)
)
数组:
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
)
)
array\u filter
自身在数组上迭代。您不需要foreach
循环。只需传递实际数组,然后按如下所示进行相应过滤:
$filteredArray = array_filter($this->analyticsData['data'], function ($data) {
return $data['author']['user_id'] == '17';
});
array\u filter
自身在数组上迭代。您不需要foreach
循环。只需传递实际数组,然后按如下所示进行相应过滤:
$filteredArray = array_filter($this->analyticsData['data'], function ($data) {
return $data['author']['user_id'] == '17';
});
我以前试过这个。但我得到了:
非法字符串偏移量'user\u id'
@marnaels最简单的调试方法是在filter函数中打印数组。var_dump($data)。这样你就能看到你从$DATA中得到了什么我做到了。这对我来说非常奇怪,因为$data
准确地打印了用户id
@marnaels,我意识到你将错误的数组传递给了数组过滤器。更新了我的答案是的,就是这个。这是我的错误。非常感谢。我以前试过这个。但我得到了:非法字符串偏移量'user\u id'
@marnaels最简单的调试方法是在filter函数中打印数组。var_dump($data)。这样你就能看到你从$DATA中得到了什么我做到了。这对我来说非常奇怪,因为$data
准确地打印了用户id
@marnaels,我意识到你将错误的数组传递给了数组过滤器。更新了我的答案是的,就是这个。这是我的错误。非常感谢。