douwei8911 2013-02-11 20:30
浏览 27
已采纳

在XML结果中解析数组

I am having trouble understanding the proper syntax to print array results using the SimpleXMLElement. From my xml result i must ask the user to match themselves with one of the people found in the array, and am not sure what is the best way to do this.

Sample XML result:

[authentication] => SimpleXMLElement Object
    (
        [age] => SimpleXMLElement Object
            (
                [code] => 5
                [ambiguous] => SimpleXMLElement Object
                    (
                        [person] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [name] => Paul  Foreman
                                        [question] => SimpleXMLElement Object
                                            (
                                                [id] => dcalc3
                                                [prompt] => What+do+the+%3Cb%3Elast+four%3C%2Fb%3E+digits+of+your+Social+Security+Number+add+up+to%3F
                                                [answer] => 5
                                            )

                                    )

                                [1] => SimpleXMLElement Object
                                    (
                                        [name] => Paul  Foreman
                                        [question] => SimpleXMLElement Object
                                            (
                                                [id] => dcalc3
                                                [prompt] => What+do+the+%3Cb%3Elast+four%3C%2Fb%3E+digits+of+your+Social+Security+Number+add+up+to%3F
                                                [answer] => 6
                                            )

                                    )

                            )

                    )

            )

    )

Solution im looking for:

<?php
$string = $xml_result;

$xml = new SimpleXMLElement($string);

$is_age_code = $xml->authentication->{'age'}->code;

if($x_is_age_code == '5'){
// code 5 means more than one match found
    // Ask user verification question
        // If answer matches question
               // Set current user as that person
}
?>

How can i find out how many 'Persons' are in the array and identify them with a number?

  • 写回答

1条回答 默认 最新

  • dongping9475 2013-02-11 21:15
    关注

    To count the number of persons use:

    echo count($xml->authentication->age->ambiguous->person);
    

    To access the subnodes of a person use

    echo $xml->authentication->age->ambiguous->person[0]->question->prompt;
    

    or with a loop:

    foreach ($xml->authentication->age->ambiguous->person as $person) {
        echo $person->question->prompt;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里