douwei8911 2013-02-11 12: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 13: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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部