How to extract a distinct value in simple xml in php
SimpleXMLElement Object (
[@attributes] => Array (
[type] => array
)
[time-entry] => Array (
[0] SimpleXMLElement Object (
[date] => 2010-06-17
[description] => "MGIN0:internal administration"
[hours] => 7.5
[id] => 26334957
[person-id] => 4912655
[project-id] => 4437844
[todo-item-id] => SimpleXMLElement Object (
[@attributes] => Array (
[type] => integer
[nil] => true
)
)
)
[5] => SimpleXMLElement Object (
[date] => 2010-06-18
[description] => "MGPJ1:WG'07 Outreach, schedule send"
[hours] => 0.25
[id] => 26376694
[person-id] => 2962280
[project-id] => 3652412
[todo-item-id] => SimpleXMLElement Object (
[@attributes] => Array (
[type] => integer
[nil] => true
)
)
)
[6] => SimpleXMLElement Object (
[date] => 2010-06-18
[description] => "TCDM1:WG'07 Outreach, upload list"
[hours] => 0.25
[id] => 26376680
[person-id] => 2962280
[project-id] => 3652412
[todo-item-id] => SimpleXMLElement Object (
[@attributes] => Array (
[type] => integer
[nil] => true
)
)
)
[7] => SimpleXMLElement Object (
[date] => 2010-06-18
[description] => "MGPJ1: Class of 2009 Anniversary, q/c, testing "
[hours] => 0.25
[id] => 26371073
[person-id] => 2962280
[project-id] => 3652412
[todo-item-id] => SimpleXMLElement Object (
[@attributes] => Array (
[type] => integer
[nil] => true
)
)
)
I want to extract distict value from given information.Here there is a sample of 4 entries,out of which 3 are of same person and 1 is开发者_运维百科 of single person.So i want to extract the single distinct entry. Pls help.I am new to php and not knowing the syntax properly.
i don't quite see the structure of your simplexml object, but you'll need to count the entries with unique person-id s...
$entries = array();
foreach ($object->{'time-entry'} as $entry) {
if (!isset($entries[$entry->{'person-id'}])) {
$entries[$entry->{'person-id'}] = array();
}
$entries[$entry->{'person-id'}][] = $entry;
}
$theEntryYouNeed = null;
foreach ($entries as $personId => $entryArray) {
if (count($entryArray) == 1) {
$theEntryYouNeed = array_pop($entryArray);
break;
}
}
精彩评论