【问题标题】:Using "RecursiveArrayIterator class" for count items in complex array recursive使用“RecursiveArrayIterator 类”计算复杂数组递归中的项
【发布时间】:2016-08-31 19:45:00
【问题描述】:

我需要使用 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] => 
)

我的新解决方案的树类:

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

(对我来说很复杂,我没有什么经验)

谢谢

【问题讨论】:

  • 你在说什么数组?
  • 是的,我忘了显示数组说话,我编辑了问题。感激不尽。
  • 请在此处粘贴您的代码。
  • 使用内联代码更新
  • @Raank 请在下面查看我的答案。你认为我的回答可以暗示你的问题吗?如果有任何让您感到困惑的地方,请告诉我们,我们会尽力为您提供帮助。

标签: php laravel multi-level


【解决方案1】:

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

【讨论】:

  • 统计网络上的用户总数是个好主意。它有助于你的想法。我有一个很沉重的方式,这个想法相当缓解要求。谢谢。
【解决方案2】:

这是将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,否则在迭代期间将被跳过。请参考RecursiveIteratorIteratorRecursiveArrayIterator 了解更多信息。

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

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

【讨论】:

  • 他的想法也成立,我根据你的回答搜索了一些我需要的解决方案,谢谢。
  • 如果您认为我的回答帮助您找到了解决方案,请不要忘记点赞。
【解决方案3】:

数组中用户的解决方案: (父系)

/**
 * @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);
}

感谢大家的帮助

【讨论】:

    猜你喜欢
    • 2014-12-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2017-07-15
    相关资源
    最近更新 更多