douli6605 2010-06-22 16:42 采纳率: 100%
浏览 40

如何在php中的简单xml中提取一个不同的值

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.

  • 写回答

1条回答 默认 最新

  • dongsi1944 2010-06-22 18:34
    关注

    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;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?