duanli3277 2015-09-15 12:50
浏览 48

如何在php中解析数组

I have to parse following array in php. I am sending an XML file through HTML file and then I got the following array. I have to parse this array to get key values from it. I am sending xml file that will be converted to array and then i have to parse the array. I am using foreach loop for this but didn't able to get inner values like Phone numbers etc

I am using

echo "<pre>";print_r($_FILES);echo "</pre>";
$myXMLData = trim(file_get_contents('php://input'));
//echo 'post action ==== >>> '.$_POST["action"];

    if ($_FILES["fileToUpload"]["error"] == 0)
    {
        $filename = $_FILES["fileToUpload"]["tmp_name"];
        //$handle = fopen($filename, "r");
        $xml = simplexml_load_file($filename);    
        //echo "<pre>";print_r($handle);echo "</pre>";
        //$xml=simplexml_load_string($handle) or die("Error: Cannot create object");
        echo "<pre>";print_r($xml);echo "</pre>";
        echo "abcdef";
        //echo $xml['fileToUpload']['name'] . "<br />";
        foreach($xml as $key=>$value)
        {
            echo $key."</br>";
            if ($key == 'Contact')
            {
                echo "abcContactttttt";
            }
            elseif($key == 'HomeInsuranceQuote')
            {
                echo "safszdfzhomeinsurance";
            }
        }

        //$xmlDataArray =  (array) $xml;
        //echo "<pre>";print_r($xml);echo "</pre>";

    }
    else{
         echo "File not uploaded successfully ..... " ;
    }


Array output is:- 
  SimpleXMLElement Object
    (
        [Contact] => SimpleXMLElement Object
            (
                [FirstName] => John
                [LastName] => Doe
                [Address1] => 123 Main
                [City] => Denver
                [State] => CO
                [ZipCode] => 80202
                [EmailAddress] => test@bankrateinsurance.com
                [PhoneNumbers] => SimpleXMLElement Object
                    (
                        [PhoneNumber] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [PhoneNumberValue] => 3030000000
                                        [PhoneNumberType] => Home
                                    )

                                [1] => SimpleXMLElement Object
                                    (
                                        [PhoneNumberValue] => 3030000001
                                        [PhoneNumberType] => Mobile
                                    )

                                [2] => SimpleXMLElement Object
                                    (
                                        [PhoneNumberValue] => 3030000002
                                        [PhoneNumberType] => Work
                                    )

                            )

                    )

                [ResidenceStatus] => Unknown
            )

        [AutoInsuranceQuoteRequest] => SimpleXMLElement Object
            (
                [Persons] => SimpleXMLElement Object
                    (
                        [Person] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [FirstName] => John
                                        [LastName] => Doe
                                        [BirthDate] => 1980-01-01
                                        [Occupation] => Architect
                                        [HighestDegree] => Graduate
                                        [CreditHistory] => SimpleXMLElement Object
                                            (
                                                [Bankruptcy] => No
                                                [CreditSelfRating] => Unsure
                                                [Repossessions] => No
                                            )

                                        [Gender] => Male
                                        [MilitaryExperience] => Yes
                                        [UsResident12Months] => Yes
                                        [MaritalStatus] => Married
                                        [RelationshipToApplication] => Self
                                        [PersonId] => 1
                                        [AgeInYears] => 34
                                        [AgeInMonths] => 415
                                    )

                                [1] => SimpleXMLElement Object
                                    (
                                        [FirstName] => Jane
                                        [LastName] => Doe
                                        [BirthDate] => 1981-01-01
                                        [Gender] => Female
                                        [MaritalStatus] => Married
                                        [RelationshipToApplication] => Spouse
                                        [PersonId] => 2
                                        [AgeInYears] => 33
                                        [AgeInMonths] => 403
                                    )

                                [2] => SimpleXMLElement Object
                                    (
                                        [FirstName] => James
                                        [LastName] => Doe
                                        [BirthDate] => 1996-01-01
                                        [Occupation] => Student
                                        [HighestDegree] => Other
                                        [Gender] => Male
                                        [RelationshipToApplication] => Child
                                        [PersonId] => 3
                                        [AgeInYears] => 19
                                        [AgeInMonths] => 206
                                    )

                            )

                    )
        [ReceivedDateTime] => 2014-06-01T00:00:00
        [Version] => 1.0
        [ApplicationId] => 12345
        [RiskType] => Premium
        [Vertical] => Auto
    )
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100