PHP SUM对象值

时间:2017-03-29 11:09:18

标签: php

当我尝试print_r( $category->child() )时,我得到了这个:(pastebin:https://pastebin.com/hSnfMpLH

Array ( [2] => IPS\forums\Forum Object ( [_url:protected] => [_childrenResults:protected] => Array ( [44ecbc64e2a074fc12588cf2dbcb2596] => Array ( ) ) [_permissions:protected] => Array ( [perm_id] => 2 [perm_view] => * [perm_2] => * [perm_3] => 3,4,6 [perm_4] => 3,4,6 [perm_5] => 3,4,6 [perm_6] => [perm_7] => ) [_originalPermissions:protected] => Array ( [perm_id] => 2 [perm_view] => * [perm_2] => * [perm_3] => 3,4,6 [perm_4] => 3,4,6 [perm_5] => 3,4,6 [perm_6] => [perm_7] => ) [noCopyButton] => [contentPostedIn:protected] => Array ( ) [_followData] => [queued:protected] => [_data:protected] => Array ( [id] => 2 [topics] => 1 [posts] => 1490710 [last_post] => 1490714195 [last_poster_id] => 1 [last_poster_name] => Evaldas [position] => 1 [password] => [password_override] => [last_title] => Welcome! [last_id] => 1 [sort_key] => last_post [show_rules] => 0 [preview_posts] => 0 [allow_poll] => 1 [inc_postcount] => 1 [skin_id] => 0 [parent_id] => 1 [redirect_url] => [redirect_on] => 0 [redirect_hits] => 0 [sub_can_post] => 1 [permission_showtopic] => 0 [queued_topics] => 0 [queued_posts] => 0 [forum_allow_rating] => 0 [min_posts_post] => 0 [min_posts_view] => 0 [can_view_others] => 1 [name_seo] => a-test-forum [seo_last_title] => welcome [seo_last_name] => dewagg [last_x_topic_ids] => [forums_bitoptions] => 0 [disable_sharelinks] => 0 [tag_predefined] => [archived_topics] => 0 [archived_posts] => 0 [ipseo_priority] => -1 [viglink] => 1 [qa_rate_questions] => * [qa_rate_answers] => * [icon] => [category_id] => 1 [app] => forums [perm_type] => forum [perm_type_id] => 2 [owner_only] => 0 [friend_only] => 0 [authorized_users] => ) [_new:protected] => [changed] => Array ( ) [skipCloneDuplication] => ) [8] => IPS\forums\Forum Object ( [_url:protected] => [_childrenResults:protected] => Array ( [44ecbc64e2a074fc12588cf2dbcb2596] => Array ( ) ) [_permissions:protected] => Array ( [perm_id] => 50 [perm_view] => * [perm_2] => * [perm_3] => 4,3,6 [perm_4] => 4,3,6 [perm_5] => 4,3,6 [perm_6] => [perm_7] => ) [_originalPermissions:protected] => Array ( [perm_id] => 50 [perm_view] => * [perm_2] => * [perm_3] => 4,3,6 [perm_4] => 4,3,6 [perm_5] => 4,3,6 [perm_6] => [perm_7] => ) [noCopyButton] => [contentPostedIn:protected] => Array ( ) [_followData] => [queued:protected] => [_data:protected] => Array ( [id] => 8 [topics] => 2 [posts] => 2 [last_post] => 1490712622 [last_poster_id] => 1 [last_poster_name] => Evaldas [position] => 7 [password] => [password_override] => [last_title] => hahahahah [last_id] => 4 [sort_key] => last_post [show_rules] => 0 [preview_posts] => 0 [allow_poll] => 1 [inc_postcount] => 1 [skin_id] => [parent_id] => 1 [redirect_url] => [redirect_on] => 0 [redirect_hits] => 0 [sub_can_post] => 1 [permission_showtopic] => 0 [queued_topics] => 0 [queued_posts] => 0 [forum_allow_rating] => 0 [min_posts_post] => 0 [min_posts_view] => 0 [can_view_others] => 1 [name_seo] => test-forum [seo_last_title] => hahahahah [seo_last_name] => dewagg [last_x_topic_ids] => [forums_bitoptions] => 8 [disable_sharelinks] => 0 [tag_predefined] => [archived_topics] => 0 [archived_posts] => 0 [ipseo_priority] => -1 [viglink] => 1 [qa_rate_questions] => * [qa_rate_answers] => * [icon] => [category_id] => 1 [app] => forums [perm_type] => forum [perm_type_id] => 8 [owner_only] => 0 [friend_only] => 0 [authorized_users] => ) [_new:protected] => [changed] => Array ( ) [skipCloneDuplication] => ) ) 1

如何总结所有主题?我试过了array_sum( $category->child()['topics'] )等,但它不起作用。抱歉我的英文不好

在stackoverflow上找到P.S:

  

或者按照@MarkBaker的建议:

     

$ sum = array_sum((array)$ myobj);

但我怎样才能选择'主题' ?

var_dump https://pastebin.com/EqJ5TPuA

新代码:

public function countingTotalTopics()
{
    foreach( $this->children() as $child )
    {
        $return = $child->topics;
    }

    return $return;
}

当我尝试$category->countingTotalTopics()时,它只给我第一个孩子的主题,我怎样才能从另一个孩子那里得到它?我想我应该在foreach中再次致电$child->countingTotalTopics(),但是如何?

1 个答案:

答案 0 :(得分:1)

您目前只获得 last 子主题。请改用此基于array_merge的功能:

public function countingTotalTopics() {
    $return = [];
    foreach( $this->children() as $child ) {
        $return = array_merge($return, (array) $child->topics);
    }
    return $return;
}