我从api
获取数据,我只需要获取相同值的数据一次。例如。 “Test Storage
”将来3次,我需要只显示它的第一个记录,并且同样适用于步行顶部和步行中间。这是xml数组的格式
我正在提取数据
SimpleXMLElement Object
(
[R_Code] => 1
[sTypeName] => Test Storage
[iDefLeaseNum] => 1
)
SimpleXMLElement Object
(
[R_Code] => 1
[sTypeName] => Test Storage
[iDefLeaseNum] => 2
)
SimpleXMLElement Object
(
[R_Code] => 1
[sTypeName] => Test Storage
[iDefLeaseNum] => 1
)SimpleXMLElement Object
(
[R_Code] => 1
[sTypeName] => Walk Middle
[iDefLeaseNum] => 1
)SimpleXMLElement Object
(
[R_Code] => 1
[sTypeName] => Walk Top
[iDefLeaseNum] => 1
)SimpleXMLElement Object
(
[R_Code] => 1
[sTypeName] => Walk Top
[iDefLeaseNum] => 1
)SimpleXMLElement Object
(
[R_Code] => 1
[sTypeName] => Walk Middle
[iDefLeaseNum] => 1
)
foreach($formatUnits->NewDataSet->Table as $unit)
{
echo $unit->iDefLeaseNum;
echo $unit->sTypeName;
}
答案 0 :(得分:0)
您必须使用array_unique()
函数从数组中删除重复的元素。
请查看此链接:http://php.net/manual/en/function.array-unique.php
答案 1 :(得分:0)
存储值并在输出之前检查它是否存在。
$arr = array();
foreach($formatUnits->NewDataSet->Table as $unit){
if(! isset($arr[$unit->sTypeName]) ){
$arr[$unit->sTypeName] = $unit->iDefLeaseNum;
echo $unit->iDefLeaseNum;
echo $unit->sTypeName;
}
}