1

Below is the structure of an array I have. I have two questions:

  1. How can I get the position of an array within an array?

For example, how would I get the position of array with element [post_id] => 2782 within [2772] => Array (the answer should be 4)?

  1. How can I get the number of children arrays within an array?

For example, how would I get the number of children arrays for the array with element [post_id] => 2779 (the answer should be 2)?

Array
(
    [2772] => Array
    (
        [post_id] => 2772
        [children] => Array
        (
            [0] => Array
            (
                [post_id] => 2774
                [children] => Array
                (
                    [0] => Array
                    (
                        [post_id] => 2779
                        [children] => Array
                        (
                            [0] => Array
                            (
                                [post_id] => 2782
                                [children] => Array
                                (
                                )
                            )

                            [1] => Array
                            (
                                [post_id] => 2781
                                [children] => Array
                                (
                                )
                            )
                        )
                    )

                    [1] => Array
                    (
                        [post_id] => 2780
                        [children] => Array
                        (
                            [0] => Array
                            (
                                [post_id] => 2784
                                [children] => Array
                                (
                                )
                            )
                        )
                    )
                )
            )

            [1] => Array
            (
               [post_id] => 2775
               [children] => Array
               (
               )
            )

            [2] => Array
            (
                [post_id] => 2776
                [children] => Array
                (
                )
            )
        )
    )
)

1 Answers1

2

You can count all children with this function:

function GetChildrenQuantity($element){
    $quantity = count($element->children);

    $childrenQuantity = 0;
    for($i=0;$i<$quantity;$i++){
       $childrenQuantity += GetChildrenQuantity($element->children[i]); 
    }

    return $quantity + $childrenQuantity;

}

You can call this function like this:

$total = GetChildrenQuantity($yourArray[2772]);

If you want to find an element use this way:

function FindElementIn($list, $id){
    $element = null;
    $quantity = count($list->children);        

    for($i=0;$i<$quantity;$i++){
        if ($list->children[i]->post_id == $id) 
            return $element->children[i];
        else {
            $element = FindElementIn($list->children[i]->children, $id); 
            if ($element != null) return $element;
        }
    }

    return null;

}
Ragnar
  • 4,393
  • 1
  • 27
  • 40