我需要计算这个数组中有多少用户,还需要使用PHP RecursiveArrayTerator库计算数组上方或下方的用户总数。
如何计算?
我目前要设置的网络(数组中的对象转换(:
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);
}
}
(对我来说很复杂,我几乎没有经验(
感谢
快速解决方案,但无需RecursiveArrayTerator:
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
以下是使用RecursiveArrayIterator
和RecursiveIteratorIterator
的解决方案。
<?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和RecursiveArrayTerator以获得更多见解。
在上面的代码段中,您必须在foreach
块之间调整代码以满足您的需要。希望这能给你一些想法。
如果您有任何困惑和疑问,请告诉我们。
阵列中的用户解决方案:(父亲关系(
/**
* @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);
}
感谢大家的帮助