0

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
AWinx
  • 11
  • 1
  • 3

5 Answers5

4

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
                )

        )

)
PPL
  • 6,357
  • 1
  • 11
  • 30
1

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.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

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>
AWinx
  • 11
  • 1
  • 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
zdenek
  • 21
  • 3
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/24710510) – TocToc Nov 28 '19 at 13:22
  • TocToc i think so question is: "How can I get output like this in PHP". so can u explain me why isnt answer for question? – zdenek Nov 29 '19 at 09:36
  • the question seems ambiguous (this is is also why you probably use "should", because you are not sure what is asked). It is ok to make assumption when the question is not sufficiently precise, but in that case you can ask for details or explicit the assumption of your answer. – TocToc Dec 02 '19 at 09:01
0

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] 
Kaleemullah
  • 446
  • 3
  • 8