Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8038157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:08:30+00:00 2026-06-05T03:08:30+00:00

I have the following code, and I’d like to get away from the call-time

  • 0

I have the following code, and I’d like to get away from the call-time pass-by-reference, (To convert from 5.2 to 5.3) but I’m not sure exactly sure what the correct way to do this would be (class, global variable, ?)

Here is a codepad that should have everything in it
http://codepad.org/ombgFPMR

<?php

function count_things($item, $key, $total) {
    $total++;
}

$counts = array(100 => 1,
            101 => 1,
            102 => array(
                106 => 1,
                107 => 1
            ),
            103 => 1,
            104 => 1,
            105 => array(
                108 => 1,
                109 => array(
                    110 => 1,
                    111 => 1,
                    112 => 1
                )
            )
        );

foreach($counts as $key => $count) {
    $total = 0;

    if(is_array($count)) {
            $total++;
            /* The below is a logic error. Array elements that contain arrays do not get
               the callback function called on them. Therefore, any children with children
               of their own will not be counted. In the output of this paste,
               the final key, $final_counts[105]['total'], should have a value of 6, but it
               actually has a value of 5. */
    array_walk_recursive($count, 'count_things', &$total);
} else {
            $total = $count;
    }

    $final_counts[$key]['total'] = $total;
}   

print_r($final_counts);

?>

Output looks like:

Array
(
[100] => Array
    (
        [total] => 1
    )

[101] => Array
    (
        [total] => 1
    )

[102] => Array
    (
        [total] => 3
    )

[103] => Array
    (
        [total] => 1
    )

[104] => Array
    (
        [total] => 1
    )

[105] => Array
    (
        [total] => 5
    )

)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:08:31+00:00Added an answer on June 5, 2026 at 3:08 am

    You can use count with COUNT_RECURSIVE flag.

    You should use closures for this, these were introduced in 5.3.0 so they should work.

    <?php
    
    $counts = array(
        100 => 1,
        101 => 1,
        102 => array(
            106 => 1,
            107 => 1
        ),
        103 => 1,
        104 => 1,
        105 => array(
            108 => 1,
            109 => array(
                110 => 1,
                111 => 1,
                112 => 1
            )
        )
    );
    
    $final_counts = array();
    
    foreach($counts as $key => $count) {
    
        if(is_array($count)) {
            $total = 1;
            array_walk_recursive($count, function() use (&$total) {
                $total++;
            });
        } else {
            $total = $count;
        }
    
        $final_counts[$key]['total'] = $total;
    }
    
    print_r($final_counts);
    

    I might be able to provide a better solution if you put your problem in context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code for loading image from url in xml parsing endElement method
I have following code written to run NSTimer . But the NSTimer selector is
I have following code: public abstract class Operand<T> { public T Value { get;
I have following code which call a task after every 20 min and its
Have following code: public interface ITest { string St1 { get; } } public
I have following code.. $query = SELECT quote, author FROM quotes ORDER BY id
I have following code and it returns false for @B LIKE @A comparison in
I have following code, after five tries (wrong words like dfasfasfb) the application stops.
I have following code which works fine in iPhone application but in iPad I
i have following code in which, i am fetching the data from the sqlite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.