我想计算一个元值在某个页面(id)内的次数。 我已经准备好了像这样的元值:
function aantal_reviews_ophalen() {
$get_page_ID = get_queried_object_id();
$get_review_count = get_post_meta( $get_page_ID, $key = 'rwp_rating_0' , $single = false);
return array_count_values($get_review_count)['343'];
}
在这种情况下,343 是rating_post_id字段的值,但不起作用。
我尝试了很多事情,但似乎无济于事。
我也通过以下示例进行了尝试:
$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
return $counts['Ben'];
像魅力一样工作,但这是因为此数组与我从$ get_review_count获得的数组不同。
当我print_r($get_review_count);
时,我得到了这个结果:
Array ( [0] => Array ( [rating_id] => rwp_rating_5b4da83f6b281 [rating_post_id] => 343 [rating_review_id] => 0 [rating_score] => Array ( [0] => 2 [1] => 2 [2] => 3.5 ) [rating_user_id] => 0 [rating_user_name] => Joep van Dongen [rating_user_email] => testmail@gmail.com [rating_title] => Dit is een test beoordeling [rating_comment] => Dit is een test beoordeling [rating_images] => Array ( ) [rating_date] => 1531815999 [rating_status] => published [rating_verified] => [rating_template] => rwp_template_5b16a33aa16b7 [rating_privacy] => Array ( ) ) [1] => Array ( [rating_id] => rwp_rating_5bd0271605392 [rating_post_id] => 343 [rating_review_id] => 0 [rating_score] => Array ( [0] => 5 [1] => 5 [2] => 5 ) [rating_user_id] => 4 [rating_user_name] => [rating_user_email] => [rating_title] => Harry de Vries [rating_comment] => Dit is een testreview om te kijken of et werkrty [rating_images] => Array ( ) [rating_date] => 1540368150 [rating_status] => published [rating_verified] => [rating_template] => rwp_template_5b16a33aa16b7 [rating_privacy] => Array ( [t5bab7c0abed1f] => true ) ) )
这看起来像一个嵌套数组,但是老实说,我不知道如何检索我想要的数据。
如何计算此数组中[rating_post_id]的值为343的次数并返回该值?
编辑 好的,我现在用这个:
echo var_export($get_review_count, true);
这就是我回来的东西:
array ( 0 => array ( 'rating_id' => 'rwp_rating_5b4da83f6b281', 'rating_post_id' => 343, 'rating_review_id' => '0', 'rating_score' => array ( 0 => 2.0, 1 => 2.0, 2 => 3.5, ), 'rating_user_id' => 0, 'rating_user_name' => 'Joep van Dongen', 'rating_user_email' => 'dongen.van.joep@gmail.com', 'rating_title' => 'Dit is een test beoordeling', 'rating_comment' => 'Dit is een test beoordeling', 'rating_images' => array ( ), 'rating_date' => 1531815999, 'rating_status' => 'published', 'rating_verified' => false, 'rating_template' => 'rwp_template_5b16a33aa16b7', 'rating_privacy' => array ( ), ), 1 => array ( 'rating_id' => 'rwp_rating_5bd0271605392', 'rating_post_id' => 343, 'rating_review_id' => '0', 'rating_score' => array ( 0 => 5.0, 1 => 5.0, 2 => 5.0, ), 'rating_user_id' => 4, 'rating_user_name' => '', 'rating_user_email' => '', 'rating_title' => 'Harry de Vries', 'rating_comment' => 'Dit is een testreview om te kijken of et werkrty', 'rating_images' => array ( ), 'rating_date' => 1540368150, 'rating_status' => 'published', 'rating_verified' => false, 'rating_template' => 'rwp_template_5b16a33aa16b7', 'rating_privacy' => array ( 't5bab7c0abed1f' => 'true', ), ), )
答案 0 :(得分:1)
考虑您拥有$get_review_count
这样的数组:
$get_review_count = [
[
'rating_id' => 'rwp_rating_5b4da83f6b281',
'rating_post_id' => 343 ,
'rating_review_id' => 0 ,
'rating_score' => [
'0' => 2 ,
'1' => 2 ,
'2' => 3.5 ],
'rating_user_id' => 0 ,
'rating_user_name' => 'Joep van Dongen ',
'rating_user_email' => 'testmail@gmail.com ',
'rating_title' => 'Dit is een test beoordeling ',
'rating_comment' => 'Dit is een test beoordeling ',
'rating_images' => [],
'rating_date' => 1531815999 ,
'rating_status' => 'published ',
'rating_verified' => '',
'rating_template' => 'rwp_template_5b16a33aa16b7 ',
'rating_privacy' => []
],
[
'rating_id' => 'rwp_rating_5b4da83f6b281',
'rating_post_id' => 343 ,
'rating_review_id' => 0 ,
'rating_score' => [
'0' => 2 ,
'1' => 2 ,
'2' => 3.5 ],
'rating_user_id' => 0 ,
'rating_user_name' => 'Joep van Dongen ',
'rating_user_email' => 'testmail@gmail.com ',
'rating_title' => 'Dit is een test beoordeling ',
'rating_comment' => 'Dit is een test beoordeling ',
'rating_images' => [],
'rating_date' => 1531815999 ,
'rating_status' => 'published ',
'rating_verified' => '',
'rating_template' => 'rwp_template_5b16a33aa16b7 ',
'rating_privacy' => []
],
];
由于它是多维数组,您可以使用array_column
仅限制'rating_post_id'
列,然后再进行array_count_values
:
echo array_count_values(array_column($get_review_count, 'rating_post_id'))['343'];
答案 1 :(得分:1)
如果将array_column与array_intersect结合使用,则会隔离ID为343的完整阵列。
$new = array_intersect_key($arr,array_intersect(array_column($arr, 'rating_post_id'), [343]));
var_dump($new);
echo count($new); //2