Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count multidimensional array values?

I am building an MLM software in PHP, one of its function is to count the downline performance. It creates an array from parent-child relationships, which can be seen below. How can I get the performance (array key: points) of my children, and their grandchildren as well through the fifth down level?

    Array
(
    [children] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => Medvedev
                    [email] => 
                    [points] => 7
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 3
                                    [name] => Putin
                                    [email] => 
                                    [points] => 4
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 5
                                                    [name] => Nathan
                                                    [email] => 
                                                    [points] => 3
                                                )

                                            [1] => Array
                                                (
                                                    [id] => 7
                                                    [name] => George
                                                    [email] => 
                                                    [points] => 666
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 4
                                    [name] => Lucas
                                    [email] => 
                                    [points] => 43
                                )

                        )

                )
        )

    [id] => 27
    [name] => Boss
    [email] => 
    [points] => 99999
)
like image 660
Gábor DANI Avatar asked Mar 12 '26 13:03

Gábor DANI


1 Answers

this should work with unlimited depth starting from a main array like

$array = array(
    'children' => array( /* ADD HERE INFINITE COMBINATION OF CHILDREN ARRAY */ ),
    'id' => #,
    'name' => '',
    'email' => '',
    'points' => #
);

function recursive_children_points($arr) {
    global $hold_points;
    if (isset($arr['points'])) {
        $hold_points[] = $arr['points'];
    }
    if (isset($arr['children'])) {
        foreach ($arr['children'] as $children => $child) {
            recursive_children_points($child);
        }
    }
    return $hold_points;
}

$points = recursive_children_points($array);
print "<pre>";
print_r($points);
/**
     // OUTPUT
     Array
(
    [0] => 99999
    [1] => 7
    [2] => 4
    [3] => 3
    [4] => 666
    [5] => 43
)
**/
    print "<pre>";
like image 123
Luca Filosofi Avatar answered Mar 14 '26 03:03

Luca Filosofi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!