0

我有以下数组:

$users
: array = 
  0: array = 
    0: object(Person) = 
      strName: string = John
      strLastName: string = Smith
      strAge: string = 19
    1: object(Person) = 
      strName: string = Jean
      strLastName: string = Newton
      strAge: string = 22
  1: array = 
    0: object(Person)= 
      strName: string = John
      strLastName: string = Smith
      strAge: string = 19
    1: object(Person) = 
      strName: string = Jean
      strLastName: string = Newton
      strAge: string = 22
  2: array = 
    0: object(Person) = 
      strName: string = Martin
      strLastName: string = Marin
      strAge: string = 25    

为了摆脱我所做的重复:

$users = array_map("unserialize", array_unique(array_map("serialize", $users)));

我得到:

$users
: array = 
  0: array = 
    0: object(Person) = 
      strName: string = John
      strLastName: string = Smith
      strAge: string = 19  
    1: object(Person) = 
      strName: string = Jean
      strLastName: string = Newton
      strAge: string = 22  
  1: array = 
    0: object(Person) = 
      strName: string = Martin
      strLastName: string = Marin                                                                                                        
      strAge: string = 25   

现在我必须获取数组包含的信息。我正在做:

$i=0;
foreach ($users as $u)
{
  echo $u [$i]->getName()); 
.
.
  $i++;
 }

但我没有打印数组的所有元素,$u [2] 是 NULL。

我做错了什么?

万分感谢!

4

2 回答 2

0

你有嵌套数组:你需要嵌套循环。

foreach ($users as $ulist) {
    foreach ($ulist as $u) {
        echo $u->getName(); 
    }
}
于 2012-11-21T09:53:28.590 回答
0

尝试

$i=0;
foreach ($users as $u)
{
  while ($u [$i] != NULL ){
echo $u [$i]->getName(); 
.
.
  $i++;
}
$i=0
 }

你试图循环,但没有重置 $i

更精确 :

数组调用

0 0
0 1
1 2   << does not exist   => $u [2] = Null
1 3
于 2012-11-21T09:54:53.523 回答