-5

我正在使用 amazon api 检索要存储在数据库中的演员列表。我不断收到重复的条目,并希望有人能在可能的情况下帮助我。

这是我正在做的

$i = $result->Items->Item->ItemAttributes->Actor;
echo "Actor :"; $alist = array(); foreach ($i as $a) {  $alist[] = $a; print_r($alist);  }

他就是结果。

Array ( [0] => SimpleXMLElement Object ( [0] => Tommy Lee Jones ) ) Array ( [0] => SimpleXMLElement Object ( [0] => Tommy Lee Jones ) [1] => SimpleXMLElement Object ( [0] => John Goodman ) ) Array ( [0] => SimpleXMLElement Object ( [0] => Tommy Lee Jones ) [1] => SimpleXMLElement Object ( [0] => John Goodman ) [2] => SimpleXMLElement Object ( [0] => Peter Sarsgaard ) ) Array ( [0] => SimpleXMLElement Object ( [0] => Tommy Lee Jones ) [1] => SimpleXMLElement Object ( [0] => John Goodman ) [2] => SimpleXMLElement Object ( [0] => Peter Sarsgaard ) [3] => SimpleXMLElement Object ( [0] => Kelly Macdonald ) ) Array ( [0] => SimpleXMLElement Object ( [0] => Tommy Lee Jones ) [1] => SimpleXMLElement Object ( [0] => John Goodman ) [2] => SimpleXMLElement Object ( [0] => Peter Sarsgaard ) [3] => SimpleXMLElement Object ( [0] => Kelly Macdonald ) [4] => SimpleXMLElement Object ( [0] => Mary Steenburgen ) ) 

任何帮助,将不胜感激。

干杯

4

2 回答 2

1

将您的 print_r() 函数移到 foreach 循环之外

echo "Actor :";
$alist = array();
foreach ($i as $a) {
  $alist[] = $a;
}
print_r($alist);

您的输出随着每次迭代而增长

Array ( [0] => SimpleXMLElement Object ( [0] => Tommy Lee Jones ) )
Array ( [0] => SimpleXMLElement Object ( [0] => Tommy Lee Jones ) [1] => SimpleXMLElement Object ( [0] => John Goodman ) )
Array ( [0] => SimpleXMLElement Object ( [0] => Tommy Lee Jones ) [1] => SimpleXMLElement Object ( [0] => John Goodman ) [2] => SimpleXMLElement Object ( [0] => Peter Sarsgaard ) )
Array ( [0] => SimpleXMLElement Object ( [0] => Tommy Lee Jones ) [1] => SimpleXMLElement Object ( [0] => John Goodman ) [2] => SimpleXMLElement Object ( [0] => Peter Sarsgaard ) [3] => SimpleXMLElement Object ( [0] => Kelly Macdonald ) )
Array ( [0] => SimpleXMLElement Object ( [0] => Tommy Lee Jones ) [1] => SimpleXMLElement Object ( [0] => John Goodman ) [2] => SimpleXMLElement Object ( [0] => Peter Sarsgaard ) [3] => SimpleXMLElement Object ( [0] => Kelly Macdonald ) [4] => SimpleXMLElement Object ( [0] => Mary Steenburgen ) ) 

如果您更好地格式化代码/输出,您可以更轻松地发现问题

于 2013-02-15T08:49:21.640 回答
0

您可以将名称值设置为数组的键;这样重复的名称将覆盖数组中的前一个键。

$alist[$a->name] = $a

您必须从 SimpleXMLElement 对象中检索字符串中的名称

我希望我理解你的问题,这些提示会有所帮助

这将删除重复的条目到您的数组中,而不是重复的 print_r 与鞋相同的数组,多一个行吃时间

于 2013-02-15T08:53:55.410 回答