0

i have this output i want one function to count all children thanks im using php version 7.1

Array
(
    [0] => stdClass Object
        (
            [slug] => home
            [name] => home
            [children] => Array
                (
                    [0] => stdClass Object
                        (
                            [slug] => contact-us
                            [name] => Contact Us
                            [children] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [slug] => new
                                            [name] => new
                                            [children] => Array
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [slug] => km
                                                            [name] => km
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

i want one function to count all children thanks im using php version 7.1

3 Answers3

1

A recursive function will give the answer by calling itself each time it detects children:

<?php

$data = array (
  0 => 
  (object) array(
     'slug' => 'home',
     'name' => 'home',
     'children' => 
    array (
      0 => 
      (object) array(
         'slug' => 'contact-us',
         'name' => 'Contact Us',
         'children' => 
        array (
          0 => 
          (object) array(
             'slug' => 'new',
             'name' => 'new',
             'children' => 
            array (
              0 => 
              (object) array(
                 'slug' => 'km',
                 'name' => 'km',
              ),
            ),
          ),
        ),
      ),
    ),
  ),
);

// expects an array as input parameter, per your sample data
function myCount(array $data) {
    // static variable keeps its value, similar to a global but not available outside function
    static $count = 0;
    
    // this actually counts sibling's children, too.  If you want just the first child, use $data[0]
    foreach($data as $d) {
        // each array contains an object, so check if the object has the property 'children'
        if(property_exists($d,'children')) {
            
            // call the function again to see how many children this child has
            myCount($d->children);
            $count++; // count here to only count children
        }
        // counts siblings and children
        // $count++;  
    }
    return $count;
}

print myCount($data);

Working sample at http://sandbox.onlinephpfunctions.com/code/b6769a58b617926ba9daaa1399c4fdda56fab225

Tim Morton
  • 2,614
  • 1
  • 15
  • 23
0

Php array count recursive


echo count($array, COUNT_RECURSIVE);

Be careful your code is has stdClass ,you must that convert to array


 $array=json_decode(json_encode($yourdata), true);

echo count($array, COUNT_RECURSIVE);

dılo sürücü
  • 3,821
  • 1
  • 26
  • 28
0
function count_array($a){
        $count = 0;
        foreach ($a as $key => $value) {
            if(is_array($value)){
                $count += count_array($value);
            }else if($value instanceof stdClass)
                $count += count_array(json_decode(json_encode($value), true));
            else{
                ++$count;
            } 
        }
        
        return $count;
    }
laguf1.mobi
  • 171
  • 6
  • Right idea, but you're counting the objects as well as the arrays. This solution came up with a count of 8. – Tim Morton Nov 17 '20 at 22:16