简体   繁体   English

计数嵌套数组元键的值

[英]Count values of nested array metakey

I want to count how many times an metavalue is inside a certain page (id). 我想计算一个特定页面(id)内一个元值有多少次。 I allready got the metavalue like this: 我已经准备好了像这样的元值:

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 in this case is the value of the field rating_post_id but not working. 在这种情况下, 343是字段rating_post_id的值,但不起作用。

I tried many things but nothing seems to work. 我尝试了很多事情,但似乎无济于事。

I also tried it with this example: 我也通过以下示例进行了尝试:

$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
return $counts['Ben'];

Working like a charm but this is because this array is different then the array I got from $get_review_count. 像魅力一样工作,但这是因为此数组与我从$ get_review_count获得的数组不同。

When I print_r($get_review_count); 当我print_r($get_review_count); I get this as a result: 我得到这个结果:

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

This looks like a nested array but to be honest I have no clue how to retrieve the data that I want. 这看起来像一个嵌套数组,但老实说,我不知道如何检索所需的数据。

How can I count how many times [rating_post_id] with value 343 is in this array and return the value? 如何计算此数组中具有值343的[rating_post_id]次并返回该值?

EDIT Ok, I now used this: 编辑好吧,我现在用这个:

echo var_export($get_review_count, true);

And this is the what I got back: 这就是我得到的:

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', ), ), )

Consider you have $get_review_count array like this : 考虑您有这样的$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' => []
    ],
];

Since it's multidimensional array, you could use array_column to limit the 'rating_post_id' column only, then do the array_count_values afterwards : 由于它是多维数组,因此您可以使用array_column仅限制'rating_post_id'列,然后再执行array_count_values

echo array_count_values(array_column($get_review_count, 'rating_post_id'))['343'];

If you combine the array_column with array_intersect you will isolate the full arrays with id 343. 如果将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

https://3v4l.org/AWDEf https://3v4l.org/AWDEf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM