simplexml_load_file - foreach shows one time loop - php

I have a table structure like this, I need to swing her foreach loop to extract all 848 elements. The problem is that the loop displays only one item instead of the actual amount sitting there. With the way with regular forem and idid substitution works, although writing the same thing is in my opinion longer and uglier.
Array structure:
SimpleXMLElement Object
(
[#attributes] => Array
(
[language] => pol
[currency] => PLN
)
[product] => Array
(
[0] => SimpleXMLElement Object
[#attributes] => Array
(
[id] => 8
)
[1] => SimpleXMLElement Object
[#attributes] => Array
(
[id] => 4
)
[etc...]
)
I try this way
$chairXML = simplexml_load_file('toparser.xml');
foreach ($chairXML->children() as $children) {
echo $children->product['id'];
}
But this doesn't work, loop return one record.

$xml = '
<xml>
<product>
<cherry>1</cherry>
<apple>3</apple>
<orange>4</orange>
</product>
</xml>';
$simpleXmlObj = simplexml_load_string($xml);
foreach ($simpleXmlObj->product->children() as $children) {
print_r($children);
}
result
SimpleXMLElement Object
(
[0] => 1
)
SimpleXMLElement Object
(
[0] => 3
)
SimpleXMLElement Object
(
[0] => 4
)
I think your problem in $simpleXmlObj->children()
Try $simpleXmlObj->product->children()

Related

How to check array object and array?

First array:
[VariationSpecificsSet] => SimpleXMLElement Object
(
[NameValueList] => Array
(
[0] => SimpleXMLElement Object
(
[Name] => Size
[Value] => Array
(
[0] => 5FT King Size
[1] => 4FT6 Double
)
)
[1] => SimpleXMLElement Object
(
[Name] => Main Colour
[Value] => Array
(
[0] => Brown
[1] => Black
)
)
)
)
Second Array:
[Variation] => SimpleXMLElement Object
(
[StartPrice] => 14.99
[Quantity] => 12
[VariationSpecifics] => SimpleXMLElement Object
(
[NameValueList] => SimpleXMLElement Object
(
[Name] => Size
[Value] => No.10-1M
)
)
)
examine above two arrays
i want to store value NameValueList in database but the problem is sometimes it is SimpleXMLElement Object and sometimes it is Array
how can i store them ...??
You can detect is by is_array().
$myVal=$test['NameValueList'];
if(is_array($myVal) && count($myVal)>0){
foreach($myVal as $item){
echo $item->Name.":".echo $item->Value;
}
} else {
echo $myVal->Name.":".echo $myVal->Value;
}
Did you tried using json_encode like below.
You can convert the object to array.
$array=json_decode(json_encode($object),true);

PHP XML to Array/Object with Attributes and Values

AI have an XML which have attributes as well as values in them. I want to convert it to an Array or Array Object along with attributes and values.
XML
<?xml version="1.0" encoding="UTF-8"?>
<itemBody>
<div label="options">
<optionchoices optionIdentifier="RESPONSE" shuffle="false" maxOptions="1">
<choice identifier="A1"><![CDATA[aaaa]]></choice>
<choice identifier="A2"><![CDATA[bbbb]]></choice>
<choice identifier="A3"><![CDATA[cccc]]></choice>
</optionchoices>
</div>
</itemBody>
I tried two set of code but the result was not as expected.
Code 1
<?php
$xml = simplexml_load_file('test.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>
Output
SimpleXMLElement Object
(
[div] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => options
)
[optionchoices] => SimpleXMLElement Object
(
[#attributes] => Array
(
[optionIdentifier] => RESPONSE
[shuffle] => false
[maxOptions] => 1
)
[choice] => Array
(
[0] => aaaa
[1] => bbbb
[2] => cccc
)
)
)
)
In the above output if we check then in choice node we get the values only and not the attributes
Code 2
<?php
$xml = simplexml_load_file('test.xml');
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>
Output
SimpleXMLElement Object
(
[div] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => options
)
[optionchoices] => SimpleXMLElement Object
(
[#attributes] => Array
(
[optionIdentifier] => RESPONSE
[shuffle] => false
[maxOptions] => 1
)
[choice] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A2
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A3
)
)
)
)
)
)
In this output we get only attributes of XML.
Now what I want is to obtain Attributes as well as Values of the XML.
Please help.
Thanks in advance.
This is what I got. And this is the solution which I expected.
http://outlandish.com/blog/xml-to-json/

How to retrieve value from SimpleXMLElement Object

After calling the prestashop webservice, I recieved the response as shown below:
SimpleXMLElement Object
(
[order] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 2
)
)
)
)
I tried to retrive the content by looping as shown below:
foreach($resources as $resource){
echo '<pre>';
print_r($resource['id']);
echo '</pre>';
}
This gives me:
SimpleXMLElement Object
(
[0] => 1
)
SimpleXMLElement Object
(
[0] => 2
)
How can I retrieve 1 and 2 which are the values of these objects? thanks
I hate simplexml...
<?php
$xml = file_get_contents('xml.xml');
$xml = new SimpleXMLElement($xml);
foreach($xml as $key => $value)
{
$attrs = $value->attributes();
foreach($attrs as $attr_k => $attr_v)
echo $attr_k.": ".$attr_v."\n";
}

Looping through array and create a new array of query results PHP

I have a PHP array object that can contain zero or more values like this:
Array
(
[0] => stdClass Object
(
[id] => dkgasO05P2XpfyWW
)
[1] => stdClass Object
(
[id] => LzE6G9UQIShOUoKq
)
)
I want to loop through each value in this array and use the id in a query that returns an object that looks like this:
Array
(
[0] => stdClass Object
(
[id] => taWPlKGXHR5Y03cc
[title] => Test Document Title
[filename] => test.docx
)
)
On each iteration of the loop the query returns with one result in the form of an array object. I want to add the object to an array object that in this case would look something like this:
Array
(
[0] => stdClass Object
(
[id] => dkgasO05P2XpfyWW
[title] => Test Document Title 0
[filename] => test0.docx
)
[1] => stdClass Object
(
[id] => LzE6G9UQIShOUoKq
[title] => Test Document Title 1
[filename] => test1.docx
)
)
The query is written and working and I know I need to use a foreach loop to iterate over the array of IDs, but I don't quite get how to set it up so that the end result is an array object as listed just above. I'm using PHP & Codeigniter to do all of this.
The code of the foreach I have so far is something like this:
$child = array();
foreach ($id as $row) {
$child = $this->users_model->get_docnfo($id);
}
Thanks for reading!
You should try it with
$child = array();
foreach ($id as $row) {
$child[] = $this->users_model->get_docnfo($row->id);
}
Note the $row->id instead of $id and also the brackets after $child.

Select elements other then a specific one form Xml Object

I am parsing the array given below. Looping through td using foreach. Now i want to select the value other than [#attributes]. I cannot use a or p specifically as they change through out the objects.
How can i achieve this?
[0] => SimpleXMLElement Object
(
[th] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rowspan] => 2
[scope] => row
)
[p] => Memory
)
[td] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => ttl
)
[a] => Card slot
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => nfo
)
[p] => No
)
)
)
Want the solution to work in php.
Try below one
<?php
foreach($td as $element)
{
foreach($element as $key => $value)
{
if(!preg_match("/#/", $key) && !is_array($value))
echo $element[$key];
}
}
?>

Categories