如何获取数组中的每个用户列表

public function getChildren($parent, $level=0,$getlevel=array()) { 


    $criteria = new CDbCriteria;

    $criteria->condition='referred_by_user_id=:id';

    $criteria->params=array(':id'=>$parent);

    $count = array(0=>0);

    $model = $this->findAll($criteria);


    // $levelcount = array();

    $user=User::model()->findbypk(Yii::app()->user->id);

    if($user->level!=$level)

    {

         foreach ($model as $key) {

            $count[0]++;            

            $index=1;

            // echo str_repeat(' — ', $level) . $key->name . "<br />";

            $children= $this->getChildren($key->id, $level+1);

            // pr($children);die;

            foreach ($children as $child)

            {

                // pr($child);

                if ($child==0)

                  continue;

                if (isset($count[$index]))

                  $count[$index] += $child;

                else    

                  $count[$index] = $child;

                $index++;

            }

        }

    }

   return $count; 

}

我只能输出用户数。如何获取数组中每个级别的用户 ID?


30秒到达战场
浏览 143回答 2
2回答

千万里不及你

我前段时间做了类似的事情:public static function getChildren($idParent) {&nbsp; &nbsp; $elem&nbsp; &nbsp; &nbsp; = [];&nbsp; &nbsp; $childrens = Arbol::model()->findAll("estado = 1 and id_parent = " . $idParent . ' ORDER BY level');&nbsp; &nbsp; foreach ($childrens as $key => $child) {&nbsp; &nbsp; &nbsp; &nbsp; $persona = Persona::model()->findbyPk($child->id_child);&nbsp; &nbsp; &nbsp; &nbsp; if ((int) $persona->id_superior === $idParent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $nombre&nbsp; &nbsp; &nbsp;= explode(" ", $persona->nombres);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $elem[$key] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id"&nbsp; &nbsp; => $persona->id_persona,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image" => Yii::app()->baseUrl . '/images/user-icon' . ($persona->pago_inscripcion ? '-activo.png' : '.png'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "text"&nbsp; => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title' => $nombre[0] . ' ' . substr($persona->apellido_paterno, 0, 1) . '.'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $keySUperior = array_search($persona->id_superior, array_column($elem, "id"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (is_int($keySUperior)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $elem[$keySUperior]["children"] = self::getChildren((int) $persona->id_superior);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $elem;}我希望我能给你任何帮助

临摹微笑

我认为问题是你必须在 for 之外创建 $index 变量
打开App,查看更多内容
随时随地看视频慕课网APP