Access array returned from xpath in PHP - 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

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

Multidimensional array in PHP?

How to read specific values from multi dimensional array using php below is the array. I want to read values of [itemBody][div][0][p].
SimpleXMLElement Object
(
[#attributes] => Array
(
[toolName] => Eqiat
[toolVersion] => 0.7~git
[adaptive] => false
[timeDependent] => false
[identifier] => ITEM_b95e4391e33aa28561b01493b6e328f8
[title] => match the following
)
[stylesheet] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://gauss.ecs.soton.ac.uk/eqiat/eqiat.css
[type] => text/css
[title] => Eqiat item styles, designed to override QTIEngine's output where appropriate
)
)
[responseDeclaration] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_0
[cardinality] => multiple
[baseType] => identifier
)
[correctResponse] => SimpleXMLElement Object
(
[value] => question_0_option_3
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_1
[cardinality] => multiple
[baseType] => identifier
)
[correctResponse] => SimpleXMLElement Object
(
[value] => question_1_option_2
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_2
[cardinality] => multiple
[baseType] => identifier
)
[correctResponse] => SimpleXMLElement Object
(
[value] => question_2_option_1
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_3
[cardinality] => multiple
[baseType] => identifier
)
[correctResponse] => SimpleXMLElement Object
(
[value] => question_3_option_0
)
)
)
[outcomeDeclaration] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
[cardinality] => single
[baseType] => integer
)
[defaultValue] => SimpleXMLElement Object
(
[value] => 0
)
)
[itemBody] => SimpleXMLElement Object
(
[div] => Array
(
[0] => SimpleXMLElement Object
(
[p] => Match The Following
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => eqiat-emi
)
[ol] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => emioptions
)
[li] => Array
(
[0] => bangalore
[1] => india
[2] => tiger
[3] => onion
)
)
[choiceInteraction] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[maxChoices] => 0
[minChoices] => 0
[shuffle] => false
[responseIdentifier] => RESPONSE_question_0
)
[prompt] => vegetable
[simpleChoice] => Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[maxChoices] => 0
[minChoices] => 0
[shuffle] => false
[responseIdentifier] => RESPONSE_question_1
)
[prompt] => animal
[simpleChoice] => Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[maxChoices] => 0
[minChoices] => 0
[shuffle] => false
[responseIdentifier] => RESPONSE_question_2
)
[prompt] => country
[simpleChoice] => Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[maxChoices] => 0
[minChoices] => 0
[shuffle] => false
[responseIdentifier] => RESPONSE_question_3
)
[prompt] => city
[simpleChoice] => Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
)
)
)
)
)
[responseProcessing] => SimpleXMLElement Object
(
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[baseValue] => 0
)
[responseCondition] => Array
(
[0] => SimpleXMLElement Object
(
[responseIf] => SimpleXMLElement Object
(
[match] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_0
)
)
[correct] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_0
)
)
)
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[sum] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
)
[baseValue] => 1
)
)
)
)
[1] => SimpleXMLElement Object
(
[responseIf] => SimpleXMLElement Object
(
[match] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_1
)
)
[correct] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_1
)
)
)
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[sum] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
)
[baseValue] => 1
)
)
)
)
[2] => SimpleXMLElement Object
(
[responseIf] => SimpleXMLElement Object
(
[match] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_2
)
)
[correct] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_2
)
)
)
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[sum] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
)
[baseValue] => 1
)
)
)
)
[3] => SimpleXMLElement Object
(
[responseIf] => SimpleXMLElement Object
(
[match] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_3
)
)
[correct] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_3
)
)
)
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[sum] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
)
[baseValue] => 1
)
)
)
)
)
)
)
Try:
$element = $object->itemBody->div->0->p;
That is an object and itemBody is a property that holds another object with a property div which is an array of objects with properties, p being one for the object at array index 0:
$result = $object->itemBody->div[0]->p;
First convert the complete object in to array and then access
' function object2array($object) { '
return json_decode(json_encode($object), true);
}
$data = object2array(simplexml_load_string($xml1));
echo '',print_r($data),'';`
Now i can access by echo $data[itemBody][div][0][p];

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'];
}

How to get the name and values from xml array using 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

Categories