i have an array structure like this (output by 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 )))
how can i get output like this in PHP:
0, walson 986, john 01, merry 234 1, nelson 987, richard 01, joan 345 2, danny 989, soffie 02, roland 345
thank you,
You can use php function like
$simple = simplexml_load_string($xml);
$arr = json_decode( json_encode($simple) , 1);
print_r($arr);
this will give you array result like
Array
(
[items] => Array
(
[0] => Array
(
[walson] => 986
[john] => 01
[merry] => 234
)
)
)
Using XML is quite easy once you understand how to use the proper API's, with SimpleXML it is easy to access the structure of the data using object notation (->items
in the code accesses the <items>
elements).
$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);
This uses a couple of loops to access each element at a time, the inner loop just reads each element and creates a key/value pair from the element name and contents.
I got it. The point is how i can get the key and value from such output array of simpleXMLElement Object:
$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>";
}
some of original xml - more than 6000 records (edited):
<?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>