How to get an xml element by index - php

I have an xml file that contains a list of elements. I want to retrieve a specific element by index. I looked around for a while and didn't find anything that worked for me.
I would have thought this would work, but it doesn't:
echo($xml->children()[0]);
I'm not very experienced in php, coming from a C# background, so any help is appreciated.

Array dereferencing was added in PHP 5.4 so your code will (probably) work with that version.
If you're stuck with an older version, simply try this
$children = $xml->children();
echo $children[0];

Related

Determine if PHP installation preg_* functions support multibyte regular expressions [duplicate]

Is there any way to get version (and date of release) of PCRE bundled with PHP from PHP code and store it into variable?
I can found it using phpinfo() but can't find any other way to get that value directly from code.
I was trying to find solution last couple of hours but it's hopeless.
So far, I can get complete phpinfo() output in variable and pull out PCRE version/release date from there but I'm wondering is there easier solution?
You can also use constant PCRE_VERSION
found source here
I think the ReflectionExtension class is made for this, though I can't seem to get the version out of it directly (getVersion() returns null). This does work however:
$pcreReflector = new ReflectionExtension("pcre");
ob_start();
$pcreReflector->info();
$pcreInfo = ob_get_clean(); // Version and release date can be parsed from here
You'll still have to parse it, but at least it's just the relevant part and not the entire phpinfo output.

Create comma separated string via xml values

I'm working on some system for a few hours now and this little thing is too much for me to think logically about at the moment.
Normally I would wait a few hours but this is a last minute job and I need to finish this.
Here's my problem:
I have an XML file that gets posted to my PHP file, the PHP file inserts certain data into a DB, but some XML nodes have the same name:
<accessoires>
<accessoire>value1</accessoire>
<accessoire>value2</accessoire>
<accessoire>value3</accessoire>
</accessoires>
Now I want to get a var $acclist which contains all values seperated by a comma:
value1,value2,value3,
I bet the solution to this is very easy but I'm at the known point where even the easiest piece of code becomes a hassle. And googling only comes up with nodes that in some way have their own identifiers.
Could someone help me out please?
You can try simplexml_load_string to parse the html then call implode on the node after casting to an array.
NOTE This code was tested in php 5.4.6 and behaves as expected.
<?php
$xml = '<accessoires>
<accessoire>value1</accessoire>
<accessoire>value2</accessoire>
<accessoire>value3</accessoire>
</accessoires>';
$dat = simplexml_load_string($xml);
echo implode(",",(array)$dat->accessoire);
For 5.3.x I had to change to
$xml = '<accessoires>
<accessoire>value1</accessoire>
<accessoire>value2</accessoire>
<accessoire>value3</accessoire>
</accessoires>';
$dat = simplexml_load_string($xml);
$dat = (array)$dat;
echo implode(",",$dat["accessoire"]);
You do this by taking a library that is able to parse and process XML, for example with SimpleXML:
implode(',', iterator_to_array($accessoires->accessoire, FALSE));
The key part here is to use iterator_to_array() as SimpleXML offers the same-named child-elements here as an iterator. Otherwise $accessoires->accessoire gives you auto-magically only the first element (if any).

foreach array loop does not always work on Windows

I encounter this very odd scenario. The PHP is below:
$complete_categories_list=array();
foreach ($modules_library_processed as $key=>$modules_categories_rendered) {
foreach ($modules_categories_rendered['modulecategories'] as $module_category_key=>$module_category_values) {
foreach ((array) $module_category_values as $module_key_inner=>$module_category_value) {
$complete_categories_list[]=trim($module_category_value);
}
}
}
This is working perfectly in Unix/Linux localhost and in some instances will work in Windows localhost. But not all of them. The problem is that at the end of the loop, $complete_categories_list is empty even though $modules_library_processed has some data on it. $modules_library_processed is a very large array, so I cannot post the entire data here. I hope someone can shed light on this.
UPDATE: The source is an XML file
simplexml_load_string($modules_xml_exported); which I converted to an array. The result is $modules_library_processed.
print_r($module_category_values) results to SimpleXMLElement Object ()
But I typecasted it to an array..so that should work right?
I think I have fixed the problem The root cause is the script processing SimpleXML which does not play when the script is run under Windows even though with all the Type casting, etc. I ended up rewriting my scripts so it won't output any SimpleXML element but all in array/string format. I hope this helps someone. Thanks.

Trouble with XML vs JSON data

Using PHP, I'm trying to grab data from the Goodreads API, which returns XML. I've been having a hell of a time trying to figure out how to pull data out of it.
At some point in the adventure, someone suggested I do a json decode( json encode ($blah)) of the whole thing and use JSON instead of XML.
That brings me to my current situation. Everything works as it should, up to the point where I'm pulling data out of the returned array. I probably should have spent more time reading and learning about arrays, but after more than two days of doing every Google search I could think of, I came here.
Here's the entirety of what gets returned by Goodreads:
a:2:{s:7:"Request";a:3:{s:14:"authentication";s:4:"true";s:3:"key";a:0:{}s:6:"method";a:0:{}}s:6:"search";a:7:{s:5:"query";a:0:{}s:13:"results-start";s:1:"1";s:11:"results-end";s:1:"1";s:13:"total-results";s:1:"1";s:6:"source";s:9:"Goodreads";s:18:"query-time-seconds";s:4:"0.06";s:7:"results";a:1:{s:4:"work";a:9:{s:11:"books_count";s:1:"7";s:2:"id";s:7:"5024045";s:24:"original_publication_day";s:2:"16";s:26:"original_publication_month";s:1:"9";s:25:"original_publication_year";s:4:"2008";s:13:"ratings_count";s:3:"227";s:18:"text_reviews_count";s:2:"53";s:14:"average_rating";s:4:"4.33";s:9:"best_book";a:5:{s:2:"id";s:7:"4958245";s:5:"title";s:37:"7 Habits of Happy Kids [With Earbuds]";s:6:"author";a:2:{s:2:"id";s:5:"38343";s:4:"name";s:10:"Sean Covey";}s:9:"image_url";s:56:"http://photo.goodreads.com/books/1343744353m/4958245.jpg";s:15:"small_image_url";s:56:"http://photo.goodreads.com/books/1343744353s/4958245.jpg";}}}}}
What I want from this array is the "id" variable appears under "best_book". For that, I'm using these lines of code:
$goodreads_results = json_decode(json_encode((array) simplexml_load_string($goodreads_data_a)), 1);
$goodreads_id = $goodreads_results->search->results->work->best_book->id;
I should point out that the array I posted (that began "a:2:{s:7") is what's contained in $goodreads_results after the above two lines of code. So I know everything UP TO that point works as it should.
For whatever reason, I'm not getting the ID. The $goodreads_id variable is empty.
Can somebody help me figure out why? Even though I know it's likely something basic, I'm lost, and everything is starting to look the same to me.
Try:
<?php
$goodreads_data_a = 'a:2:{s:7:"Request";a:3:{s:14:"authentication";s:4:"true";s:3:"key";a:0:{}s:6:"method";a:0:{}}s:6:"search";a:7:{s:5:"query";a:0:{}s:13:"results-start";s:1:"1";s:11:"results-end";s:1:"1";s:13:"total-results";s:1:"1";s:6:"source";s:9:"Goodreads";s:18:"query-time-seconds";s:4:"0.06";s:7:"results";a:1:{s:4:"work";a:9:{s:11:"books_count";s:1:"7";s:2:"id";s:7:"5024045";s:24:"original_publication_day";s:2:"16";s:26:"original_publication_month";s:1:"9";s:25:"original_publication_year";s:4:"2008";s:13:"ratings_count";s:3:"227";s:18:"text_reviews_count";s:2:"53";s:14:"average_rating";s:4:"4.33";s:9:"best_book";a:5:{s:2:"id";s:7:"4958245";s:5:"title";s:37:"7 Habits of Happy Kids [With Earbuds]";s:6:"author";a:2:{s:2:"id";s:5:"38343";s:4:"name";s:10:"Sean Covey";}s:9:"image_url";s:56:"http://photo.goodreads.com/books/1343744353m/4958245.jpg";s:15:"small_image_url";s:56:"http://photo.goodreads.com/books/1343744353s/4958245.jpg";}}}}}
';
$goodreads_results = unserialize($goodreads_data_a);
echo $goodreads_id = $goodreads_results['search']['results']['work']['best_book']['id'];
?>

How to get version of PCRE (bundled with PHP) from code?

Is there any way to get version (and date of release) of PCRE bundled with PHP from PHP code and store it into variable?
I can found it using phpinfo() but can't find any other way to get that value directly from code.
I was trying to find solution last couple of hours but it's hopeless.
So far, I can get complete phpinfo() output in variable and pull out PCRE version/release date from there but I'm wondering is there easier solution?
You can also use constant PCRE_VERSION
found source here
I think the ReflectionExtension class is made for this, though I can't seem to get the version out of it directly (getVersion() returns null). This does work however:
$pcreReflector = new ReflectionExtension("pcre");
ob_start();
$pcreReflector->info();
$pcreInfo = ob_get_clean(); // Version and release date can be parsed from here
You'll still have to parse it, but at least it's just the relevant part and not the entire phpinfo output.

Categories