I'm getting an error while looping over an element children:
$placeById=simplexml_load_file("http://www.43places.com/service/get_place_by_id?api_key=1234&id=".$placeId);
$children=$placeById->children;
echo '<ul>';
foreach ($children as $child)
{
echo '<li>'.$child->child.'</li>';
}
echo '</ul>';
My Output is only this:
Alabama
Although there are several children, see the XML here
Your only referencing the first child, try this:
$placeById=simplexml_load_file("http://www.43places.com/service/get_place_by_id?api_key=1234&id=".$placeId);
$children=$placeById->children;
echo '<ul>';
foreach ($children->child as $child) {
echo '<li>'.$child.' - '. $child['id'].'</li>';
}
echo '</ul>';
Related
I'm getting the node names of an XML using this code:
$url = 'https://www.toptanperpa.com/xml.php?c=shopphp&xmlc=e7ef2a0122';
$xml = simplexml_load_file($url) or die("URL Read Error");
echo $xml->getName() . "<br>";
foreach ($xml->children() as $child) {
echo $child->getName() . "<br>";
foreach ($child->children() as $child2) {
echo $child2->getName() . "<br>";
foreach ($child2->children() as $child3) {
echo $child3->getName() . "<br>";
foreach ($child3->children() as $child4) {
echo $child4->getName() . "<br>";
}
}
}
}
I'm getting the nodes and children correctly, however, it's duplicating.
Result is as below:
urunler
urun
urun_aktif
urun_metaKeywords
urun_metaDescription
urun_url
urun
urun_aktif
urun_metaKeywords
urun_metaDescription
urun_url
Should I just use array_unique or is there a better method?
Thanks
i used recursive function this is simple
function getChildrens($x) {
$children = array();
array_push($children, $x->getName());
foreach ($x->children() as $chld) {
$children = array_merge($children, getChildrens($chld));
}
return array_unique($children);
}
echo implode("<br>", getChildrens($xml));
I'm Trying to format the output of an array using php but I can't seem to get the keys and values on the same line. I've listed the code that I'm using to display the keys and values but this code outputs the keys and values on different lines
function olLiTree($tree)
{
echo '<pre>';
foreach($tree as $key => $item) {
if (is_array($item)) {
echo '<pre>', $key ;
olLiTree($item);
echo '</pre>';
} else {
echo '<pre>', $item, '</pre>';
}
}
echo '</ul>';
}
print(olLiTree($results));
Use <ul> <li> .... </li></ul>. Also remove comma(,) because PHP use dot(.) for concat string.
function olLiTree($tree)
{
echo '<ul>';
foreach($tree as $key => $item) {
if (is_array($item)) {
echo '<li>'. $key ;
olLiTree($item);
echo '</li>';
} else {
echo '<li>' .$item. '</li>';
}
}
echo '</ul>';
}
print(olLiTree($results));
You use comma , instead of dot .
for string concatenation php use dot
function olLiTree($tree)
{
echo '<pre>';
foreach($tree as $key => $item) {
if (is_array($item)) {
echo '<pre>'. $key ;
olLiTree($item);
echo '</pre>';
} else {
echo '<pre>' . $item. '</pre>';
}
}
echo '</ul>';
}
print(olLiTree($results));
<?php
$xml = "<articles>
<article id=\"18357302\">
<articleCategories>
<articleCategory id=\"default\"/>
<articleCategory id=\"66607\"/>
</articleCategories>
</article>
</articles>";
$feed = simplexml_load_string($xml);
$items = $feed->article;
foreach ($items as $article) {
// $categorie = $article->articleCategories->articleCategory[id];
$categories = $article->articleCategories;
print_r($categories);
echo "<br>print_r indeed returns an array, but impossible to echo it using foreach!!!<br>";
foreach ($categories->id as $category) {
if ($category != "default") {
echo $category;
}
}
}
?>
not sure what i am doing wrong, i am just trying to find a way to remove the part with the default value inside articlesCategories
<articleCategory id=\"default\"/>
The script needs to ignore this part and just use the next articleCategory from the XML file, and i would prefer to avoid removing it with regex
The script iterates over articleCategories tag.
But it needs to iterate over articleCategory tag.
The following changes will be enough.
foreach ($categories->articleCategory as $category) {
if ($category["id"] != "default") {
echo $category["id"];
}
}
I have a multi dimensional array with the structure:
Name
Address
Area
Area Type 1
Area Type 2
Area Type 2
I have a loop that grabs the Name and Address for each business and echos them out. Now what I want to be able to do is now grab the area and chuck that out for each business. My loop thus far is as follows:
foreach ($data AS $key => $value) {
echo '<ul>';
echo '<li>';
echo $value['Name'];
echo '</li>';
echo '<li>';
echo $value['Address'];
echo '</li>';
echo '<li>';
foreach ($data as $row) {
echo $data['Area'];
}
echo '</li>';
echo '</ul>';
}
I can output all the Areas in one go with:
foreach($data as $row)
{
foreach($row['Area'] as $areaout)
{
echo $areaout;
}
}
But I need it to echo out with it's respective name and address
Shouldn't you simply replace
foreach ($foo as $row) {
echo $foo['Area'];
}
by
echo '<ul>';
foreach ($value['Area'] as $v) {
echo '<li>' . $v . '</li>';
}
echo '</ul>'
?
I am rebuilding a nodes children by saving them out to an array as strings, trashing them in the XML, inserting a new child node into the array as a string... now I want to loop through the array and write them back out to the original node. The problem is I can't find anything on how to add a child node using a string.
See below for my code. Thanks!!!
$xml = simplexml_load_file($url);
$questionGroup = $xml->qa[intval($id)];
$children = array(); // create empty array
foreach ($questionGroup->children() as $element) { // loop thru children
array_push($children, $element->asXML()); // save XML into array
}
//unset($questionGroup->answer);
//unset($questionGroup->question);
//create new node
$newNode = '<answer><title>'.$title.'</title><description>'.$description.'</description><subName>'.$subName.'</subName><date>'.$date.'</date><timestamp>'.$timestamp.'</timestamp></answer>';
echo "children count: ".count($children);
echo "<br /><br />";
print_r($children);
echo "<br /><br />";
// insert new
array_splice($children,intval($elementIndex),0,$newNode);
echo "children count: ".count($children);
echo "<br /><br />";
print_r($children);
echo "<br /><br />";
echo $questionGroup->asXML();
foreach ($children as $element) { // loop thru array
echo "<br /><br />";
echo $element;
//$questionGroup->addChild(simplexml_load_string($element)); // add array element to the empty questionGroup
}
echo "<br /><br />";
echo "questionGroup: ".$questionGroup;
UPDATE:
I found a function that I modified and was able to get working:
function append_simplexml(&$simplexml_to, &$simplexml_from)
{
$childNode = $simplexml_to->addChild($simplexml_from->getName(), "");
foreach ($simplexml_from->children() as $simplexml_child)
{
$simplexml_temp = $childNode->addChild($simplexml_child->getName(), (string) $simplexml_child);
foreach ($simplexml_child->attributes() as $attr_key => $attr_value)
{
$simplexml_temp->addAttribute($attr_key, $attr_value);
}
// append_simplexml($simplexml_temp, $simplexml_child);
}
}
With this usage in my foreach() loop:
foreach ($children as $element) { // loop thru array
append_simplexml($questionGroup, new SimpleXMLElement($element));
}
You can't do that with SimpleXML alone. There is a nice way to do this with the DOM extension and the DOMDocumentFragment class. (Please note that I didn't try to understand your logic in the example provided, but you should be able to implement the simple sample below into your code).
$xml = simplexml_load_string('<root><parent/></root>');
// get the parent node under which you want to insert your XML fragement
$parent = dom_import_simplexml($xml->parent);
// create the XML fragment
$fragment = $parent->ownerDocument->createDocumentFragment();
// append the XML literal to your fragment
$fragment->appendXML('<child id="1"/><child id="2"><grandchild/></child>');
// append the fragment to the parent node
$parent->appendChild($fragment);
echo $xml->asXML();
/*
* <?xml version="1.0"?>
* <root><parent><child id="1"/><child id="2"><grandchild/></child></parent></root>
*/
Links:
dom_import_simplexml()
DOMDocument::createDocumentFragment()
DOMDocumentFragment::appendXML()
DOMNode::appendChild()
SimpleXMLElement::asXML()
I found a function that I modified and was able to get working:
function append_simplexml(&$simplexml_to, &$simplexml_from)
{
$childNode = $simplexml_to->addChild($simplexml_from->getName(), "");
foreach ($simplexml_from->children() as $simplexml_child)
{
$simplexml_temp = $childNode->addChild($simplexml_child->getName(), (string) $simplexml_child);
foreach ($simplexml_child->attributes() as $attr_key => $attr_value)
{
$simplexml_temp->addAttribute($attr_key, $attr_value);
}
// append_simplexml($simplexml_temp, $simplexml_child);
}
}
With this usage in my foreach() loop:
foreach ($children as $element) { // loop thru array
append_simplexml($questionGroup, new SimpleXMLElement($element));
}
You should be able to do something like this:
$child = new SimpleXMLElement($newNode);
$yourxmlobject->addChild($child);