How to get the name and values from xml array using php - php

Here is my Xml code:
SimpleXMLElement Object
(
[resultsRows] => SimpleXMLElement Object
(
[row] => Array
(
[0] => SimpleXMLElement Object
(
[dimension] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => date
[value] => 20140102
[label] => Date
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => browserType
[value] => Chrome
[label] => Browser Type
)
)
)
[metric] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => visitDuration
[value] => 1242
[label] => Avg. Visit Duration
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => bounces
[value] => 3
[label] => Bounces
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => repeatVisitors
[value] => 0
[label] => Repeat Visitors
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => newVisitors
[value] => 5
[label] => New Visitors
)
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => visits
[value] => 10
[label] => Visits
)
)
[5] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => pageViews
[value] => 66
[label] => Page Views
)
)
)
)
)
)
)
Above Xml array need to print like key and values type.The array showing like dimension and metric.I want print the values inside #attributes nested array like key and value.
Thanks.

Found in the comments on php SimpleXml documentation:
function xml2array ( $xmlObject, $out = array () )
{
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;
return $out;
}
Check the docs here: http://cl1.php.net/ref.simplexml

Related

How to use a variable string with array in a foreach loop

I try to get the following code working.
foreach ($uniqueItems as $key => $value) {
$output = "{$value->properties->property[10]->value}";
echo $output;
}
In the browser I see 24.99
Above code gives me the correct output. But I need the $output from outside this foreach. I try the following
$output = "{\$value->properties->property[10]->value}";
foreach ($uniqueItems as $key => $value) {
echo $output;
}
In the browser I see {$value->properties->property[10]->value}
This is print_r($uniqueItems[$key]);
SimpleXMLElement Object
(
[name] => Donnay joggingbroek zwart unisex
[properties] => SimpleXMLElement Object
(
[property] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => deliveryTime
)
[value] => Voor 16.00 uur besteld, morgen in huis!
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => brand
)
[value] => Donnay
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => size
)
[value] => SimpleXMLElement Object
(
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => color
)
[value] => Zwart
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => EAN
)
[value] => SimpleXMLElement Object
(
)
)
[5] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => categoryPath
)
[value] => Tenniskleding/Tenniskleding dames
)
[6] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => deliveryCosts
)
[value] => 4.95
)
[7] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => discount
)
[value] => 5.00
)
[8] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => subcategories
)
[value] => Tenniskleding dames
)
[9] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => SKU
)
[value] => 489000-TL-020
)
[10] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => fromPrice
)
[value] => 24.99
)
))
How to get the right output?
The second snippet won't work as php does not know that $value is to be interpolated until $value gets defined which is happening when the loop actually starts. Till then, it will just be a string. That's why it's just printing out it as a string

extracting xml #attributes php

I'm trying to extract data property[value] from xml, the $xml->xpath("/products") shows:
[categories] => SimpleXMLElement Object ( [category] => Zapas )
[properties] => SimpleXMLElement Object (
[property] =>
Array (
[0] => SimpleXMLElement Object ( [#attributes] =>
Array (
[name] => VAL)
[value] => 1111111 )
[1] => SimpleXMLElement Object ( [#attributes] =>
Array (
[name] => manufacturer )
[value] => Nike)
[2] => SimpleXMLElement Object ( [#attributes] =>
Array (
[name] => condition )
[value] => new )
[3] => SimpleXMLElement Object ( [#attributes] =>
Array (
[name] => deliveryCosts )
[value] => 0.00 )
[4] => SimpleXMLElement Object ( [#attributes] =>
Array (
[name] => stock )
[value] => in stock )
) )
I'm using next code for extracting xml values:
$xml = simplexml_load_file("http://productType=2",'SimpleXMLElement');
foreach($xml->product as $file){
$file->categories->category;
$file->properties????
}
The problem: I can't extract e.g Nike from:
[properties] => SimpleXMLElement Object (
[property] =>
Array (
[0] => SimpleXMLElement Object ( [#attributes] =>
Array (
[1] => SimpleXMLElement Object ( [#attributes] =>
Array (
[name] => manufacturer )
[value] => Nike)...
using
$brand= (string)$file->properties->property[1]->attributes()
I got manufacturer data (name), but
$brand= (string)$file->properties->property[1]->attributes()->value
doesn't works for getting Nike (value)
How could I get property[value] data?
Thanks in advance

Access array returned from xpath in PHP

How can I access (in order to display) the elements returned by xpath?
My xpath call is
$products = $this->_dom->xpath('//menu/category[#name="'.$category.'"]');
and what I got is
Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => pizza ) [item]
=> SimpleXMLElement Object ( [#attributes] => Array ( [name] => Tomato and Cheese ) [type] =>
Regular [available] => true [size] => Array ( [0] => SimpleXMLElement Object ( [#attributes]
=> Array ( [name] => Small ) [price] => 5.50 ) [1] => SimpleXMLElement Object ( [#attributes]
=> Array ( [name] => Large ) [price] => 9.75 ) ) ) ) [1] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => pizza ) [item] => SimpleXMLElement Object ( [#attributes]
=> Array ( [name] => Pepperoni ) [type] => Regular [available] => true [size] => Array ( [0]
=> SimpleXMLElement Object ( [#attributes] => Array ( [name] => Small ) [price] => 6.85 ) [1]
=> SimpleXMLElement Object ( [#attributes] => Array ( [name] => Large ) [price] => 10.85 ) ) )
) [2] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => pizza ) [item] =>
SimpleXMLElement Object ( [#attributes] => Array ( [name] => Meatball ) [type] => Regular
[available] => true [size] => Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array
( [name] => Small ) [price] => 6.85 ) [1] => SimpleXMLElement Object ( [#attributes] => Array
( [name] => Large ) [price] => 10.85 ) ) ) ) [3] => SimpleXMLElement Object ( [#attributes] =>
Array ( [name] => pizza ) [item] => SimpleXMLElement Object ( [#attributes] => Array ( [name]
=> Hawaiian ) [type] => Regular [available] => true [size] => Array ( [0] => SimpleXMLElement
Object ( [#attributes] => Array ( [name] => Small ) [price] => 7.95 ) [1] => SimpleXMLElement
Object ( [#attributes] => Array ( [name] => Large ) [price] => 11.80 ) ) ) ) [4] =>
SimpleXMLElement Object ( [#attributes] => Array ( [name] => pizza ) [item] =>
SimpleXMLElement Object ( [#attributes] => Array ( [name] => Three Aces Special ) [type] =>
Speciality [available] => true [size] => Array ( [0] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => Small ) [price] => 9.80 ) [1] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => Large ) [price] => 15.80 ) ) ) ) [5] => SimpleXMLElement
Object ( [#attributes] => Array ( [name] => pizza ) [item] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => Mediterranean ) [type] => Speciality [available] => true
[size] => Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => Small )
[price] => 9.80 ) [1] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => Large )
[price] => 15.80 ) ) ) ) )
What I want to do is to display this information in a sort of a list, so I'm wondering how can I access the elements of SimpleXMLElement Object.
Thanks
Cleaning up output:
[0] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => pizza )
[item] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => Tomato and Cheese )
[type] => Regular
[available] => true
[size] => Array (
[0] => SimpleXMLElement Object (
[#attributes] => Array ([name] => Small )
[price] => 5.50 )
[1] => SimpleXMLElement Object (
[#attributes] => Array ([name] => Large )
[price] => 9.75 )
)
)
)
Something like this will work, though a lot of your data is wrapped up as attributes, which will make it a little more verbose to get to, like the sizes attribute. You may need to cast items in order to get the value.
$products = $this->_dom->xpath('//menu/category[#name="'.$category.'"]');
foreach ($products as $product) {
// accessing SimpleXML values is tricky
// $name is a SimpleXMLElement Object
$name = $product['title'];
// $name2 has the string value of the title attribute
$name2 = (string) $product['title'];
// following works because __toString is implemented
// by SimpleXMLElement class
echo "Title: " . $product['title'];
// alternative syntax, unless you cast to a
// string, you have a SimpleXML Object
$name3 = $product->attributes()->title;
}
See Access #attributes data in SimpleXMLElement in PHP

How can I separate these xml elements?

I got an xml response of
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 2.0
[msg_type] => response
)
[header] => SimpleXMLElement Object
(
[param] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => SN
[value] => 1012
)
)
...
[9] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => ErrorInfo
[value] => NA
)
)
[10] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => ProcessTime
[value] => 0
)
)
)
)
[Heartbeat] => SimpleXMLElement Object
(
)
)
I tried:
$version1 = (string) $xml->version; // expecting to get "2.0"
$errorinfo = (string) $xml->name->ErrorInfo; // expecting to get "NA"

How to loop through an array of SimpleXMLElement objects and return node default value?

I am trying to get default value for each field but I just can't figure out how to loop through all the objects. Tried with converting them to a simple array with json_decode but it just not clear as to what to loop.
Here it is:
SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => params
)
[fieldset] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Cat1
)
[field] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] =>
[type] => list
[default] => 1
)
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Item2
)
[field] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post1
[type] => text
[default] => 5
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post2
[type] => text
[default] => 18
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post3
[type] => text
[default] => 15
)
[option] => Array
(
[0] => Blue
[1] => Green
)
)
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Cat2
)
[field] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post6
[type] => text
[default] => 3
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post7
[type] => text
[default] => 36
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post7
[type] => text
[default] => 88
)
)
)
)
)
Try this (where $xml is your root object)
foreach($xml->fieldset as $fieldset) {
foreach($fieldset->field as $field) echo (string)$field['default'];
}

Categories