xml到数组转换

时间:2013-08-01 10:30:33

标签: php xml simplexml

我有以下xml,我正在转换为数组

<?xml version="1.0" encoding="utf-8"?>
<root>
  <student-No>S0001</student-No>
  <Title>Mr</Title>
  <firstName>John</firstName>
  <lastName>Doe1</lastName>
  <gender>male</gender>
  <address>australia</address>
  <mobileNumber>987654321</mobileNumber>
  <homeNumber>000-000-000</homeNumber>
  <email>dummyemail@email.com</email>
  <institute>act</institute>
  <grade>begineer</grade>
  <student-No>S0002</student-No>
  <Title>Mr</Title>
  <firstName>Sheen</firstName>
  <lastName>D</lastName>
  <gender>male</gender>
  <address>australia</address>
  <mobileNumber>987654321</mobileNumber>
  <homeNumber>000-000-001</homeNumber>
  <email>dummyemail@email.com</email>
  <institute>act</institute>
  <grade>begineer</grade>
  <student-No>S0003</student-No>
  <Title>Mr</Title>
  <firstName>Mark</firstName>
  <lastName>Webber</lastName>
  <gender>male</gender>
  <address>australia</address>
  <mobileNumber>987654321</mobileNumber>
  <homeNumber>000-000-002</homeNumber>
  <email>dummyemail@email.com</email>
  <institute>act</institute>
  <grade>begineer</grade>
  <student-No>S0004</student-No>
  <Title>Mr</Title>
  <firstName>Eric</firstName>
  <lastName>smith</lastName>
  <gender>male</gender>
  <address>australia</address>
  <mobileNumber>987654321</mobileNumber>
  <homeNumber>000-000-003</homeNumber>
  <email>dummyemail@email.com</email>
  <institute>act</institute>
  <grade>begineer</grade>
  <student-No>S0005</student-No>
  <Title>Mr</Title>
  <firstName>Larry</firstName>
  <lastName>Page</lastName>
  <gender>male</gender>
  <address>australia</address>
  <mobileNumber>987654321</mobileNumber>
  <homeNumber>000-000-004</homeNumber>
  <email>dummyemail@email.com</email>
  <institute>act</institute>
  <grade>begineer</grade>
  <student-No>S0006</student-No>
  <Title>Mr</Title>
  <firstName>Mark</firstName>
  <lastName>z</lastName>
  <gender>male</gender>
  <address>australia</address>
  <mobileNumber>987654321</mobileNumber>
  <homeNumber>000-000-005</homeNumber>
  <email>dummyemail@email.com</email>
  <institute>act</institute>
  <grade>begineer</grade>
  <student-No>S0007</student-No>
  <Title>Mr</Title>
  <firstName>william</firstName>
  <lastName>s</lastName>
  <gender>male</gender>
  <address>australia</address>
  <mobileNumber>987654321</mobileNumber>
  <homeNumber>000-000-006</homeNumber>
  <email>dummyemail@email.com</email>
  <institute>act</institute>
  <grade>begineer</grade>
  <student-No>S0008</student-No>
  <Title>Mr</Title>
  <firstName>bill</firstName>
  <lastName>G</lastName>
  <gender>male</gender>
  <address>australia</address>
  <mobileNumber>987654321</mobileNumber>
  <homeNumber>000-000-007</homeNumber>
  <email>dummyemail@email.com</email>
  <institute>act</institute>
  <grade>begineer</grade>
  <student-No>S0009</student-No>
  <Title>Mr</Title>
  <firstName>ricky</firstName>
  <lastName>p</lastName>
  <gender>male</gender>
  <address>australia</address>
  <mobileNumber>987654321</mobileNumber>
  <homeNumber>000-000-008</homeNumber>
  <email>dummyemail@email.com</email>
  <institute>act</institute>
  <grade>begineer</grade>
  <student-No>S0010</student-No>
  <Title>Mrs</Title>
  <firstName>emma</firstName>
  <lastName>Webber</lastName>
  <gender>female</gender>
  <address>australia</address>
  <mobileNumber>987654321</mobileNumber>
  <homeNumber>000-000-009</homeNumber>
  <email>dummyemail@email.com</email>
  <institute>act</institute>
  <grade>begineer</grade>
</root>     

我正在使用以下代码生成xml

$xmlObject = new SimpleXMLElement($xml);
print_r(@json_decode(@json_encode($xmlObject),1));

它正在生成以下输出作为数组

Array ( [student-No] => Array ( [0] => S0001 [1] => S0002 [2] => S0003 [3] => S0004 [4] => S0005 [5] => S0006 [6] => S0007 [7] => S0008 [8] => S0009 [9] => S0010 ) [Title] => Array ( [0] => Mr [1] => Mr [2] => Mr [3] => Mr [4] => Mr [5] => Mr [6] => Mr [7] => Mr [8] => Mr [9] => Mrs ) [firstName] => Array ( [0] => John [1] => Sheen [2] => Mark [3] => Eric [4] => Larry [5] => Mark [6] => william [7] => bill [8] => ricky [9] => emma ) [lastName] => Array ( [0] => Doe1 [1] => D [2] => Webber [3] => smith [4] => Page [5] => z [6] => s [7] => G [8] => p [9] => Webber ) [gender] => Array ( [0] => male [1] => male [2] => male [3] => male [4] => male [5] => male [6] => male [7] => male [8] => male [9] => female ) [address] => Array ( [0] => australia [1] => australia [2] => australia [3] => australia [4] => australia [5] => australia [6] => australia [7] => australia [8] => australia [9] => australia ) [mobileNumber] => Array ( [0] => 987654321 [1] => 987654321 [2] => 987654321 [3] => 987654321 [4] => 987654321 [5] => 987654321 [6] => 987654321 [7] => 987654321 [8] => 987654321 [9] => 987654321 ) [homeNumber] => Array ( [0] => 000-000-000 [1] => 000-000-001 [2] => 000-000-002 [3] => 000-000-003 [4] => 000-000-004 [5] => 000-000-005 [6] => 000-000-006 [7] => 000-000-007 [8] => 000-000-008 [9] => 000-000-009 ) [email] => Array ( [0] => dummyemail@email.com [1] => dummyemail@email.com [2] => dummyemail@email.com [3] => dummyemail@email.com [4] => dummyemail@email.com [5] => dummyemail@email.com [6] => dummyemail@email.com [7] => dummyemail@email.com [8] => dummyemail@email.com [9] => dummyemail@email.com ) [institute] => Array ( [0] => act [1] => act [2] => act [3] => act [4] => act [5] => act [6] => act [7] => act [8] => act [9] => act ) [grade] => Array ( [0] => begineer [1] => begineer [2] => begineer [3] => begineer [4] => begineer [5] => begineer [6] => begineer [7] => begineer [8] => begineer [9] => begineer ) ) 

这里它创建了所有student-no元素作为一个数组,所有Title元素作为一个数组 在得到的二维数组中。 而不是这个,我想要这样的输出,其中每一行是每个数组的元素 例如

 Array ( [0] => Array ( [student-No] => S0001 [Title] => Mr [firstName] => John [lastName] => Doe1 [gender] => male [address] => australia [mobileNumber] => 987654321 [homeNumber] => 000-000-000 [email] => dummyemail@email.com [institute] => act [grade] => begineer ) [1] => Array ( [student-No] => S0002 [Title] => Mr [firstName] => Sheen [lastName] => D [gender] => male [address] => australia [mobileNumber] => 987654321 [homeNumber] => 000-000-001 [email] => dummyemail@email.com [institute] => act [grade] => begineer ) [2] => Array ( [student-No] => S0003 [Title] => Mr [firstName] => Mark [lastName] => Webber [gender] => male [address] => australia [mobileNumber] => 987654321 [homeNumber] => 000-000-002 [email] => dummyemail@email.com [institute] => act [grade] => begineer ) [3] => Array ( [student-No] => S0004 [Title] => Mr [firstName] => Eric [lastName] => smith [gender] => male [address] => australia [mobileNumber] => 987654321 [homeNumber] => 000-000-003 [email] => dummyemail@email.com [institute] => act [grade] => begineer ) [4] => Array ( [student-No] => S0005 [Title] => Mr [firstName] => Larry [lastName] => Page [gender] => male [address] => australia [mobileNumber] => 987654321 [homeNumber] => 000-000-004 [email] => dummyemail@email.com [institute] => act [grade] => begineer ) [5] => Array ( [student-No] => S0006 [Title] => Mr [firstName] => Mark [lastName] => z [gender] => male [address] => australia [mobileNumber] => 987654321 [homeNumber] => 000-000-005 [email] => dummyemail@email.com [institute] => act [grade] => begineer ) [6] => Array ( [student-No] => S0007 [Title] => Mr [firstName] => william [lastName] => s [gender] => male [address] => australia [mobileNumber] => 987654321 [homeNumber] => 000-000-006 [email] => dummyemail@email.com [institute] => act [grade] => begineer ) [7] => Array ( [student-No] => S0008 [Title] => Mr [firstName] => bill [lastName] => G [gender] => male [address] => australia [mobileNumber] => 987654321 [homeNumber] => 000-000-007 [email] => dummyemail@email.com [institute] => act [grade] => begineer ) [8] => Array ( [student-No] => S0009 [Title] => Mr [firstName] => ricky [lastName] => p [gender] => male [address] => australia [mobileNumber] => 987654321 [homeNumber] => 000-000-008 [email] => dummyemail@email.com [institute] => act [grade] => begineer ) [9] => Array ( [student-No] => S0010 [Title] => Mrs [firstName] => emma [lastName] => Webber [gender] => female [address] => australia [mobileNumber] => 987654321 [homeNumber] => 000-000-009 [email] => dummyemail@email.com [institute] => act [grade] => begineer ) )

我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

不幸的是,XML的设计并不是特别好,并且没有元素将每个学生的属性组合在一起。你必须按顺序循环遍历所有子节点(使用->children())并在你经历时检查它们的名字(使用->getName()),每次遇到{{{}时创建一个新的“学生” 1}} node。

粗略算法可能如下所示:

<student-No>

答案 1 :(得分:0)

试试这个,它可能会起作用

<?php
      $xmlObject = new SimpleXMLElement($xml);                              
                                    $json = json_encode($xmlObject); 
                                    $array = json_decode($json,TRUE);
                                    //print_r($array);
            //Array in which all values are stored
                                    $jsonArray = array();
        //temporary arrays to store each array value
                                    $temp1 = array();
                                    $temp2 = array();
                                    $temp3 = array();
                                    foreach ($array as $values){
                                             array_push($temp1 , $values[0]);
                                             array_push($temp2 , $values[1]);
                                             array_push($temp3 , $values[2]);
                                    }
                                    $jsonArr = array($temp1 , $temp2 , $temp3);
                                    print_r($jsonArr);
                                    ?>

此输出

Array ( [0] => Array ( [0] => S0001 [1] => Mr [2] => John [3] => Doe1 [4] => male [5] => australia [6] => 987654321 [7] => 000-000-000 [8] => dummyemail@email.com [9] => act [10] => begineer ) [1] => Array ( [0] => S0002 [1] => Mr [2] => Sheen [3] => D [4] => male [5] => australia [6] => 987654321 [7] => 000-000-001 [8] => dummyemail@email.com [9] => act [10] => begineer ) [2] => Array ( [0] => S0003 [1] => Mr [2] => Mark [3] => Webber [4] => male [5] => australia [6] => 987654321 [7] => 000-000-002 [8] => dummyemail@email.com [9] => act [10] => begineer ) )

因此iam使用三个数组变量iam得到的值高达第三个数组值。如果您想要更多,可以创建。