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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大