我正在使用amazon api来检索要存储在数据库中的actor列表。我不断收到重复的条目,并希望有人可以帮助我。如果可能的话。
这就是我正在做的事情
$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 ) )
任何帮助都将不胜感激。
干杯
答案 0 :(得分: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 ) )
如果您更好地格式化代码/输出,可以更轻松地找到问题
答案 1 :(得分:0)
您可以将名称值设置为数组的键; 就像一个重复的名称将覆盖你的数组中的前一个键。
$alist[$a->name] = $a
您必须从SimpleXMLElement对象
中检索字符串中的名称我希望我理解你的问题,这个提示会有所帮助
这将删除重复的条目到你的数组而不是重复的print_r与shoe相同的数组,并且还有一个行的eatch时间