如何在php中解析数组

时间:2015-09-15 12:50:02

标签: php

我必须在php中解析以下数组。我通过HTML文件发送XML文件,然后我得到以下数组。我必须解析这个数组以从中获取键值。我发送将转换为数组的xml文件,然后我必须解析数组。我正在使用foreach循环,但没有能够获得内部值,如电话号码等

我正在使用

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 个答案:

没有答案