对复数数组中的计数项使用“RecursiveArrayIterator类”递归

时间:2016-08-31 19:45:01

标签: php laravel multi-level

我需要计算此数组中有多少用户,使用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] => 
)

新解决方案的My Tree类:

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

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

由于

3 个答案:

答案 0 :(得分:0)

快速解决方案,但没有RecursiveArrayIterator:

[HttpGet]
[Route("{someGuid:guid}")]
public HttpResponseMessage Get([FromUri] Guid? someGuid, [FromUri] int someInt)
{
    var responseData = _someService.RecordSet.AsQueryable(); // or some base query or equivalent
    if (someGuid.HasValue)
    {
       responseData = responseData.Where(x=>x.guid == someGuid);
    }

    if (someInt.HasValue)
    {
       responseData = responseData.Where(x=>x.int == someInt);
    }

    return Request.ResponseMessageFromApiEntity(responseData);
}

答案 1 :(得分:0)

以下是将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::SELF_FIRST的第二个参数中使用RecursiveIteratorIterator,否则在迭代期间将跳过该参数。请参阅RecursiveIteratorIteratorRecursiveArrayIterator以获得更多见解。

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

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

答案 2 :(得分:0)

阵列中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);
}

谢谢大家的帮助