I am fetching data from an api
and i need to fetch data for same value only once.for ex. "Test Storage
" is coming 3 times and i need to show it only first record of it and same for walk top and walk middle.This is format of xml array
I am fetching data
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;
}
Store the value and check if it exists before outputting it.
$arr = array();
foreach($formatUnits->NewDataSet->Table as $unit){
if(! isset($arr[$unit->sTypeName]) ){
$arr[$unit->sTypeName] = $unit->iDefLeaseNum;
echo $unit->iDefLeaseNum;
echo $unit->sTypeName;
}
}
You have to use array_unique()
function to remove duplicate elements from array.
Check out this link : http://php.net/manual/en/function.array-unique.php