Raank Raank - 3 months ago 12
PHP Question

Using "RecursiveArrayIterator class" for count items in complex array recursive

I need to count how many users there are in this array, also have a total of users above or below the array using PHP RecursiveArrayIterator library.

How to make this count?

What I currently have to set up the network (object convert in array):

Array
(
[left] =>
[center] => Array
(
[user_id] => 5
[name] => Leo Turner
[username] => ayla.zemlak
[email] => gottlieb.geovany@example.net
[sponsor] => 1
[indicate] => 1
[network] => Array
(
[left] =>
[center] => Array
(
[user_id] => 8
[name] => Jaiden Simonis
[username] => cgorczany
[email] => genoveva31@example.net
[sponsor] => 1
[indicate] =>
[network] => Array
(
[left] =>
[center] =>
[right] =>
)

)

[right] =>
)

)

[right] =>
)


My Tree class for the new solution:

<?php

namespace App\Helpers;

class Tree
{
public $total;
public $hasUp;
public $hasDown;
public $id;
public $name;
public $email;
public $username;
public $treeDefault;
public $network;

public function __construct( $id )
{
$this->total = 'total users';
$this->hasUp = 'get user up in array';
$this->hasDown = 'get user down in array';
$this->id = $id;
$this->name = $this->getUser('name');
$this->email = $this->getUser('email');
$this->username = $this->getUser('username');
$this->treeDefault = $this->getTreeJson( $id );
$this->network = $this->getNetwork( $id );
}

/**
* @param $field
* @return mixed
*/
public function getUser( $field )
{
$user = \App\Models\User::where( 'id', $this->id )->first();
return $user[ $field ];
}

/**
* @param null $id
* @return string
*/
public function getTreeJson( $id = null )
{
return json_encode( $this->getNetwork( $id ) );
}

/**
* Mount Tree Array
* @return array
*/
public function getNetwork( $id = null )
{
$hasId = (isset($id) ? $id : $this->id);
$network = \App\Models\Network::where( 'user_id', $hasId )->first();
$tree = json_decode( $network['users'] );
foreach( $tree as $key => $item ) {
$newTree[ $key ] = ($item->user != '' ? new Tree( $item->user ) : null);
}
return (isset($newTree) ? $newTree : null);
}
}


(Complex for me, I have little experience)

Thanks

Answer

Quick solution however without RecursiveArrayIterator:

function calc_users($array) {
    static $count = 0;

    if (isset($array['center']['network'])) {
        calc_users($array['center']['network']);
    }

    if (isset($array['left']['network'])) {
        calc_users($array['left']['network']);
    }

    if (isset($array['right']['network'])) {
        calc_users($array['right']['network']);
    }

    if (isset($array['left']['user_id'])) {
        $count++;
    }
    if (isset($array['right']['user_id'])) {
        $count++;
    }
    if (isset($array['center']['user_id'])) {
        $count++;
    }

    return $count;
}

$arr = [
    'left' =>null,
    'center' => [
        'user_id' => 1,
        'network' => [
            'left' =>null,
            'center' => [
                'user_id' => 1,
                'network' => [
                    'left' =>null,
                    'center' =>null,
                    'right' =>null,
                ]
            ],
            'right' => null
        ],
    ],
    'right' => ['user_id' => 10, 'network' => []]
];

print_r(calc_users($arr));// print 3
Comments