使用 "RecursiveArrayIterator class" 计算复杂数组递归中的项目

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

我需要使用 PHP RecursiveArrayIterator 库计算此数组中有多少用户,以及数组上方或下方的用户总数。

如何计算?

我目前必须设置的网络(数组中的对象转换):

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] => 
)

我的树 class 新的解决方案:

<?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);
    }
}

(对我来说比较复杂,经验少)

谢谢

没有 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

这是结合使用 RecursiveArrayIteratorRecursiveIteratorIterator 的解决方案。

<?php

$networks = [
    'left' => [
            'user_id' => 5,
            'name' => 'User1',
            'username' => 'ayla.zemlak',
            'email' => 'gottlieb.geovany@example.net',
            'sponsor' => 1,
            'indicate' => 1,
            'network' => [
                'left' => '',
                'center' => [
                        'user_id' => 8,
                        'name' => 'User2',
                        'username' => 'cgorczany',
                        'email' => 'genoveva31@example.net',
                        'sponsor' => 1,
                        'indicate' => '',
                        'network' => [
                            'left' => '',
                            'center' => '',
                            'right' => '',
                        ]
                    ],
                'right' => '',
            ]
        ],
    'center' => [
            'user_id' => 5,
            'name' => 'User3',
            'username' => 'ayla.zemlak',
            'email' => 'gottlieb.geovany@example.net',
            'sponsor' => 1,
            'indicate' => 1,
            'network' => [
                'left' => '',
                'center' => [
                        'user_id' => 8,
                        'name' => 'User4',
                        'username' => 'cgorczany',
                        'email' => 'genoveva31@example.net',
                        'sponsor' => 1,
                        'indicate' => '',
                        'network' => [
                            'left' => '',
                            'center' => '',
                            'right' => '',
                        ]
                    ],
                'right' => '',
            ]
        ],
    'right' => [
        'user_id' => 8,
        'name' => 'User5',
        'username' => 'cgorczany',
        'email' => 'genoveva31@example.net',
        'sponsor' => 1,
        'indicate' => '',
        'network' => [
            'left' => '',
            'center' => '',
            'right' => '',
        ]
    ],
];

$arrayIterator = new RecursiveArrayIterator($networks);
$usersIterator = new RecursiveIteratorIterator(
    $arrayIterator,
    RecursiveIteratorIterator::SELF_FIRST
);

foreach ($usersIterator as $key => $user) {
    if (in_array($key, ['left', 'center', 'right']) && is_array($user)) {
        echo $user['name'] . PHP_EOL;
    }
}

输出将是:

User1
User2
User3
User4
User5

这里的技巧是在 RecursiveIteratorIterator 的第二个参数中使用 RecursiveIteratorIterator::SELF_FIRST ,否则将在迭代期间跳过。请参阅 RecursiveIteratorIterator and RecursiveArrayIterator 以获得更多见解。

在上面的代码片段中,您必须调整 foreach 块之间的代码以满足您的需要。希望这能给你一些想法。

如果您有任何困惑、疑问,请告诉我们。

阵列中 UP 用户的解决方案: (父亲关系)

/**
 * @return array
 */
public function getHasUp()
{
    /*
     * Search current user father
     * Primary find
     */
    $qualify = Network::findQualify( $this->id );

    /*
     * Set the first list item with a first qualified father
     */
    $list[1] = $qualify;

    /*
     * Mount a list with first father
     */
    for( $i = 2; $i <= 10; $i++ ) {
        if ( isset($list[$i - 1]) ) {
            /*
             * Searches the father of item previous
             */
            $list[$i] = Network::findQualify( $list[$i - 1] );
        }
    }

    /*
     * Removes items with value null
     */
    return array_filter($list);
}

谢谢大家的帮助