从php中的SimpleXMLElement对象数组中获取键和值

时间:2018-03-27 06:45:36

标签: php arrays xml simple-xml-converter

我有这样的数组结构(由print_r(array)输出):

SimpleXMLElement Object ( 
    [items] => Array ( 
        [0] => SimpleXMLElement Object ( 
            [walson] => 986 
            [john] => 01 
            [merry] => 234 ) 
        [1] => SimpleXMLElement Object ( 
            [nelson] => 987 
            [richard] => 01 
            [joan] => 345 )))
        [2] => SimpleXMLElement Object ( 
            [danny] => 989 
            [soffie] => 02 
            [roland] => 345 )))

如何在PHP中获得这样的输出:

0, walson 986, john 01, merry 234
1, nelson 987, richard 01, joan 345
2, danny 989, soffie 02, roland 345

谢谢,

5 个答案:

答案 0 :(得分:1)

你可以使用像

这样的php函数
$simple = simplexml_load_string($xml);
$arr = json_decode( json_encode($simple) , 1);
print_r($arr);

这将为您提供像

这样的数组结果
Array
(
    [items] => Array
        (
            [0] => Array
                (
                    [walson] => 986 
                    [john] => 01
                    [merry] => 234
                )

        )

)

答案 1 :(得分:0)

一旦理解了如何使用正确的API,使用XML非常容易,使用SimpleXML,可以使用对象表示法轻松访问数据结构(代码中的->items访问<items>元素)。

$data = <<< XML
<Data>
   <items>
     <walson>986</walson>
     <john>01</john>
     <merry>234</merry>
   </items>
   <items>
     <walson>1986</walson>
     <john>101</john>
     <merry>1234</merry>
   </items>
   <items>
     <walson>2986</walson>
     <john>201</john>
     <merry>2234</merry>
   </items>
</Data>
XML;

$xml = simplexml_load_string($data);
$output = [];
$index = 0;
foreach ( $xml->items as $item )    {
    $itemData = [];
    foreach ( $item as $key => $element )   {
        $itemData[$key] = (string)$element;
    }
    echo $index++.", ".implode(", ", $itemData).PHP_EOL;
    $output[] = $itemData;
}

print_r($output);

这一次使用几个循环来访问每个元素,内部循环只读取每个元素并从元素名称和内容创建一个键/值对。

答案 2 :(得分:0)

我明白了。关键是我如何从simpleXMLElement对象的输出数组中获取键和值:

$simple=simplexml_load_file($xml_file) or die("Error: Cannot create object");
$array = get_object_vars($simple->items);

foreach($array as $key => $val)
{
  //by using output like this
  echo "key:".$key."-".$val."<br>";
}

一些原始xml - 超过6000条记录(已编辑):

<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<VFPData xml:space="preserve">
    <xsd:schema id="VFPData" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xsd:element name="VFPData" msdata:IsDataSet="true">
            <xsd:complexType>
                <xsd:choice maxOccurs="unbounded">
                    <xsd:element name="items" minOccurs="0" maxOccurs="unbounded">
                        <xsd:complexType>
                            <xsd:sequence>
                                <xsd:element name="budgyear">
                                    <xsd:simpleType>
                                        <xsd:restriction base="xsd:string">
                                            <xsd:maxLength value="4"/>
                                        </xsd:restriction>
                                    </xsd:simpleType>
                                </xsd:element>
                                <xsd:element name="doctype">
                                    <xsd:simpleType>
                                        <xsd:restriction base="xsd:string">
                                            <xsd:maxLength value="2"/>
                                        </xsd:restriction>
                                    </xsd:simpleType>
                                </xsd:element>
                                <xsd:element name="unitcode">
                                    <xsd:simpleType>
                                        <xsd:restriction base="xsd:string">
                                            <xsd:maxLength value="6"/>
                                        </xsd:restriction>
                                    </xsd:simpleType>
                                </xsd:element>
....
....
                                <xsd:element name="ibcode">
                                    <xsd:simpleType>
                                        <xsd:restriction base="xsd:string">
                                            <xsd:maxLength value="2"/>
                                        </xsd:restriction>
                                    </xsd:simpleType>
                                </xsd:element>
                            </xsd:sequence>
                        </xsd:complexType>
                    </xsd:element>
                </xsd:choice>
                <xsd:anyAttribute namespace="http://www.w3.org/XML/1998/namespace" processContents="lax"/>
            </xsd:complexType>
        </xsd:element>
    </xsd:schema>
    <items>
        <budgyear>2018</budgyear>
        <doctype>01</doctype>
        <unitcode>986860</unitcode>
...
...
        <ibcode>020</ibcode>
</items></VFPData>

答案 3 :(得分:0)

//should be something like this:

foreach ($array->items as $key => $item )    {
 echo "\n<br>".$key;
  foreach ($item as $name => $number){
    echo ", ".$name." " .$number;
  }
}
//output will be:

0, walson 986, john 01, merry 234
1, nelson 987, richard 01, joan 345
2, danny 989, soffie 02, roland 345

答案 4 :(得分:0)

enter image description here下图显示输出。它将解决您的问题。

[![<?php 
$array  = array();
$next\['items'\] = array(

  array(

    'walson' => '986' ,
            'john' => '01' ,
            'merry' => '234' 

  ),
    array(

    'nelson' => '987' ,
            'richard' => '01' ,
            'joan' => '345' 

  ),
  array(

    'danny' => '989' ,
            'soffie' => '02' ,
            'roland' => '345' 

  )

);


$array\[\] = $next;
//print_r($array);

foreach($array\[0\]\['items'\] as $key => $value ){
  $str = '';
  $c=0;
   foreach($value as $key_ =>$value_){
     if($c==0){
   $str .=$key;
       $c=1;
     }
   $str .=' '. $key_." ".$value_;
   }
  echo $str."<br>";

}

?>][1]][1]