skip to Main Content

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

Answers


  1. 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);
    
    
    Login or Signup to reply.
  2. 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;
        }
    
    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search