How to retrieve data from # xml attribute in PHP - php

Ok so I am stuck with this xml in PHP stuff. I have gotten pretty far considering its my first 3 hours into XML all together ever in my entire life.
I am having trouble pulling data from a XML thing that has # in the name. See below (obviously im not going to post the whole XML thing but u can see how i got there below that.
SimpleXMLElement Object
(
[#attributes] => Array
(
[date] => 2010-09
[reserved] => 6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
)
)
How i got there:
echo $this->General_functions->naked($xml->property[0]->availability->month[0]);
General_functions->naked is just a fast function to wrap and print_r around the given attribute.
My question is, HOW do i get the values inside #attributes cause no matter what i try i cant figure it out. Ive searched the web for a good 45 mins with no real answer.
Thanks in advance.
David

You need to use the attributes() method to get the results as another class. So, for example, to get the date attribute:
$myElement->attributes()->date
Also note that it's not a string, it's a SimpleXML attribute. If you want to get its actual value, you need to cast it to string explicitly:
(string)$myElement->attributes()->date

Access attributes of an element just as you would elements of an array:
(string) $xml->property[0]->availability->month[0]['date']
Edited to add the cast.

Related

Get specific value of array object

I have an array object below
33 => 'a:2:{
s :8:"latitude";
s:10:"39.3600586";
s:9:"longitude";
s:18:"-84.30993899999999";
}'
And here is the variable I'm using to get the value of that object
$events_location = $entries[1]['33'];
Is there a way to get just the value of either the latitude or longitude instead of everything in the single quotes?
Thanks!
What you have here is a serialized string. Unserialize it to access the key in the array:
$events_location = unserialize($entries[1]['33']);
echo $events_location['longitude'];
This should be a comment but its a bit long.
The string you have shown us looks vaguely like part of a serialized php entity. But it's not. If you try to unserialize this you'll get an error. The underlying data appears to be a coordinate - but even ignoring the syntactic errors, the semantics of the structure are wrong.
Please check the source you transcribed this from. If you have not copied the original content here please amend your question.
If you have transcibed it correctly then go speak to whoever supplied you with this data and ask them to fix it.

PHP SimpleXML #attributes behavior for nodelists

I've seen this problem a few times, and would like a definitive answer.
Given this structure in xml:
<ByteListSet>
<Byte Order="0">14</Byte>
</ByteListSet>
I am not able to access the attribute 'order'. var_dump (unsurprisingly) does not show any attributes for ByteListSet. Indeed, foreach iteration does not produce a #attributes item.
However, the following structure:
<ByteListSet>
<Byte Order="0"><Value>3729</Value></Byte>
</ByteListSet>
Results properly in ByteListSet having a child Byte that is a SimpleXmlObject which has #attributes.
I would assume that SimpleXML is indeed picking up the #attributes from the first case, but where is it keeping them? The trouble is that in the former structure, ByteListSet produces this on var_dump of ->children():
object(SimpleXMLElement)[25]
public 'Byte' => string '14' (length=2)
if I get_object_vars() on it and var_dump each, I simply get:
string '14' (length=2)
Indicating that Byte is not being returned to me as an xml object, but just as a string; as a property of the ByteList object above it.
Order="0" is there somewhere, but I don't have access to it. How do I get to it? NOTE: ->attributes() returns, as you would expect, a blank array.
(We do not control this schema, so it can't be restructured.)
You wrote:
Given this structure in xml:
<ByteListSet>
<Byte Order="0">14</Byte>
</ByteListSet>
I am not able to access the attribute 'order'.
Sure, because the attribute order does not exist. XML is case-sensitive, the attribute is named Order with uppercase O at the beginning.
Using the right attribute name allows you to access the value as outline in Example #5 in the basic examples of the SimpleXML extension you can find in the manual:
$ByteListSet = simplexml_load_string(<<<XML
<ByteListSet>
<Byte Order="0">14</Byte>
</ByteListSet>
XML
);
echo $ByteListSet->Byte['Order']; # 0
Please keep in mind that what you see in var_dump or print_r is not always what you get. This is espeically true for Simplexml (compare: How to get values of xml elements?; SimpleXML and print_r() - why is this empty?), however, also for other objects in PHP that make use of ArrayAccess, __toString() or IteratorIterator / IteratorAggregate.
Please let me know if that answers your question. You were not very specific which existing solutions you have not understood so far, so I answered your question, but I'm not 100% if I hit the nail.

Basic PHP - Getting Data Out of an stdClass Object

I have a quick question about something I imagine must be pretty easy - I've done a little research and found some links that seem promising, especially this, but it doesn't work for me for some reason.
Anyway, I made a stored procedure in MySQL and tested it with MySQL Workbench, and it works - it just adds num1 and num2 and returns the result. Now I'm trying to get it to work in PHP, but the result, instead of being an integer, is an array of one stdClass Object which contains that integer. That makes sense from the point of view of procedures that return a lot of data, but I'm having some trouble getting down to just the integer.
I run this:
CALL database.routine(2,7)
And I save the results into $var. When I run print_r($var), I get:
Array
(
[0] => stdClass Object
(
[num1+num2] => 9
)
)
So, to get past the Array part, I specifically asked for the first element in it, by running print_r($var[0]), which gets me:
stdClass Object
(
[num1+num2] => 9
)
And now I need to go one level deeper...I tried what the page I linked to above said and attempted to get to $var[0]->[num1+num2], as the field appears to be named, but that doesn't work. I've also tried a few combinations of single quotes and double quotes, but no luck. How do I get the number 9 out of this object?
Try...
$var[0]->{'num1+num2'}
Try this
$var[0]->{"num1+num2"}
or
$prop = 'num1+num2';
$var[0]->$prop;
Did you try this?
$var[0]->{'num1+num2'}
Try the following $var[0]->{'num1+num2'}
This is crazy but it might just work!
$var[0]->{'num1+num2'}

How to access array element "Array ( [#text] =>"

I had an XML that I turned into an array to sort it, now I want to save it as an XML again. This would not be a problem if I didn't have the following: [caseid] => Array ( [#text] => 885470 ...
I need: <caseid> 885470 </caseid>
Writing the DOM has been fine for the fieldname "caseid", but the fieldvalue is an array titled "#text" that I cannot figure out how to parse.
I can't search google for "#" symbols, so searching has been pretty tough.
I was able to access the number in the array by referencing it as a string. Stupid way to do it, but it works; the best solution would be to edit the array to xml conversion, but accessing array elements via strings worked too.
I was previously accessed array elements like so:
print_r($array[caseid][#text]); //<--Does not work with #
print_r($array[caseid]['#text']); //works
Again, not the prettiest, but a viable workaround.

drupal---the taxonomy array

when i print "print_r($node)" in the node.tpl.php. i get this.
taxonomy] => Array (
[1] => stdClass object (
[tid] =>1
[vid]=>1
[name]=>cms
............)
)
so from the above, I know the taxonomy is an array. the array's value is an object. That the question comes. I looked up the php manual again and again, didn't find there is a saying "the array's value can be a object" why,I can't follow the above's code well. Hope someone can explain it to me. Thank you.
What Pekka write:
echo $node["taxonomy"][1]->tid;
Is not wrong, in the sense that it works for the above example. However, since you are doing this in the node.tpl.php you probably want something more robust, than this, since it only works for nodes with the term that has id 1.
Since the array of taxonomy terms is of the format:
array(tid => term_object)
You need to know the tid to access the term object. If you however want the tid, you can just get the array key:
$tids = array_keys($node["taxonomy"]);
Now, you don't know if or how many terms there is associated to your node, as it can be changed via settings, if you did:
if (!empty($node["taxonomy"])) {
$tids = array_keys($node["taxonomy"]);
$tid = tids[0];
}
You would get the tid of the first term (the one with the lowest tid). If you know from your setup that the node can only have 1 term and since the theme you're doing this in is site specific, this will be good enough for you. Else $tids will be an array of all the tids for the node that you use for your wishes.
in other words, in php, the array's value can be anything. am i right.
Yes, an array can hold values of any data type.
how the code i should write
The "path" to access the variable you show above would be:
echo $node["taxonomy"][1]->tid;

Categories