Get key and value from an array of SimpleXMLElement object

5.2k Views Asked by At

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
5

There are 5 best solutions below

0
PPL On

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
                )

        )

)
0
Nigel Ren On

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.

0
AWinx On

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>
3
zdenek On
//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
0
Kaleemullah On

enter image description hereBelow picture is showing output. It will solve your problem.

[![<?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]