0

When I try print_r( $category->child() )I got this: (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

How can I sum all topics? I've tried: array_sum( $category->child()['topics'] ) etc, but It not works. Sorry for my bad english

P.S found on stackoverflow:

Or as suggested by @MarkBaker:

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

But how can I select 'topics' ?

var_dump https://pastebin.com/EqJ5TPuA


New code:

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

And when I try $category->countingTotalTopics() it gives me topics only from first child, how can I get it from another? I think I should call $child->countingTotalTopics() again in foreach, but how?

9
  • Instead of print_r output, provide var_dump output. Commented Mar 29, 2017 at 11:15
  • And if you can, put <pre> around it before you print/dump, this is kind of messy to read. Commented Mar 29, 2017 at 11:17
  • @trincot ok, updated Commented Mar 29, 2017 at 11:17
  • Try using: echo "<pre>".print_r( $category->child(),true )."</pre>". It may offer a better view of the structure. Commented Mar 29, 2017 at 11:22
  • Your variable is of a certain class. You should use the methods available in that class to get information. Do you have the definition of the class, or are you using a library for this topic management? If so, which? Commented Mar 29, 2017 at 11:26

1 Answer 1

1

You are currently getting only the last child's topics. Use this array_merge-based function instead:

public function countingTotalTopics() {
    $return = [];
    foreach( $this->children() as $child ) {
        $return = array_merge($return, (array) $child->topics);
    }
    return $return;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I've changed from $return = $child->topics to $return = $return + $child->topics and now everything works fine, is it good solution?
There is a small difference between + and array_merge: if the topics arrays have numerical, sequential keys, then + will not produce the desired result, but in all other cases it behaves the same way as array_merge. Maybe for array_merge to work in your case you need to cast it explicitly to array. See update. But if + works fine, then go with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.