Get key and value from an array of SimpleXMLElement object

php simplexml
php xml object to array
php object to xml
convert php to xml
xml in php

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

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
                )

        )

)

Get value from SimpleXMLElement Object, You have to cast simpleXML Object to a string. it returns element under current pointer of array, which is 0, so you will get value $fileName); $mainNode = $ xml->{'code'}; $cityArray = array(); foreach ($mainNode as $key� Got a simplexml object as follows: [Attributes] => SimpleXMLElement Object ( [key1] => value1 [key2] => value1 [key3] => value1 [list1] => Array ( [0] => Some info in here [1] => More info in here

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.

Simplexml - get numeric key for an array - PHP, How do I get the numeric array key for each <item>? AFAIK, this means the first SimpleXML object stored as a first element in the array. function SimpleXMLElement_append ($key, $value) { // check class if ((get_class ($key) == 'SimpleXMLElement') && (get_class ($value) == 'SimpleXMLElement')) { // check if the value is string value / data if (trim ((string) $value) == '') { // add element and attributes $element = $key-> addChild ($value-> getName ());

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>

How can I get values in array from simpleXML object? - PHP, Hi, Got a simplexml object as follows: [Attributes] => SimpleXMLElement Object ( [key1] => value1 [key2] => value1 [key3] => value1� So, now I dont get an array at the begining, but it seems the url are still inside another array that is inside a simplexmlelement? if I make a view source I get this: SimpleXMLElement Object ( [@attributes] => Array ( [id] => primaryAdServer )

//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

SimpleXMLElement::attributes - Manual, Tip to get a real array of all attributes of a node (not SimpleXML's object acting like an grab the value of '@attributes' key, which contains the array your after This post will show multiple ways to define key value array. Choose one that suits you the best, based on the use case. To give examples, we will be creating an array of students.

Below 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] 

SimpleXMLElement - Manual, Warning to anyone trying to parse XML with a key name that includes a hyphen ie.) You can only get to the root element via the array index method ($sxe[0]). Figuring out how to access the properties of a SimpleXmlElement object was a� The easy way to get the keys is with Object.keys(data). And likewise, the easy way to get the values is with Object.values(data). It is better though to have your data values stored in a separate

PHP SimpleXML - Get Node/Attribute Values, Return Value: It returns a SimpleXMLElement object which can be iterated over foreach ( $xml ->address[0]->attributes() as $key => $value ). Array; Plain objects also support similar methods, but the syntax is a bit different. Object.keys, values, entries. For plain objects, the following methods are available: Object.keys(obj) – returns an array of keys. Object.values(obj) – returns an array of values. Object.entries(obj) – returns an array of [key, value] pairs.

PHP, php xpath get attribute value how to get data from xml file in php <?php SimpleXMLElement Object ( [@attributes] => Array This function <?php function purgeAttributes( $object ) { foreach ( $object as $key => $value ) { if ( gettype(� Method 1: In this method, we will use Object.keys() and map() to achieve this. Approach: By using Object.keys(), we are extracting keys from the Object then this key passed to map() function which maps the key and corresponding value as an array, as described in the below example. Syntax: Object.keys(obj) Parameter:

How to remove the [@attributes] in the SimpleXMLElement Object, simplexml_import_dom — Get a SimpleXMLElement object from a DOM node. SimpleXML will return a reference to an object containing the node value and you "<pa><ch /><ch /><ch /></pa>", while in an PHP array, the key of which must� Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

Comments
  • When posting questions about XML, it's more helpful to post the original XML rather than an object dump.
  • the original xml is 6546 records
  • OK - so post a sample of the data - in the same way you've posted a sample of the output.
  • i dont create data. i just got an xml file just the way it is. its too complex. but i've just found the answer, as i wrote it below.
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
  • 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?
  • 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.