PHP SimpleXML #attributes behavior for nodelists - php

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.

Related

how can I use mongodb array(simple and embedded array) elements as php indexed array [duplicate]

I googled, installed Devel, Drupal for Firebug, but I can't find it.
I found what I want, I know where it is; I just don't know how to get it.
I'll put this in code brackets, but Devel tells me the file name (which I want to stick into the .tpl.php file) is here:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
So, how do I get that FILENAME.jpg to be output using PHP?
<?php print $something->other; ?>
Whenever you need to read a value out of a variable, you need to know which expression you need to formulate to access that value.
For a simple variable value this is simple, you just take the variable name and access it as a variable by prefixing it with the $ sign:
var_dump($variable);
This is documented here.
However this does only work for simple datatypes like string or integer. There are as well compound datatypes, namely array and object. They can contain further datatypes, be it simple or compound. You can learn in the PHP manual how to access the values of an array and how you can access them from an object. I think you already know of that a bit, so just for having it linked here.
When you have learned about that, you can then combine this. E.g. if there is an array within an object and therein is a string you would like to get, you need to combine the $ sign and the variable name with the needed accessors, property names and array keys. Then you get your value. The data you have posted shows that you have an object that has some other objects and arrays and in the end you find the variable name.
Some combination example:
var_dump($variable->handler->view[0]->_field_data);
This is based on the data you've provided above. $variable is where you start, -> is used to access object members which need to be named then (like a name for a variable) : handler. As you've seen in your debug output that handler is an object, you need to use again the -> to access the view member of it.
Now view is different because it's an array. You access values of an array by using [] and putting the key in there. The key in my example is a number, 0. And as the value of that array entry is an object again, in the next step you need to use -> again.
You can continue this game until you reach the element that you're interested in. The debug output you already have helps you to write the expression that returns the value. Possibly it is:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
But I can not validate that here on my system in full.
However when finding things out, it's helpful to make use of var_dump as you could step by step extend the expression until you find the element. If you make an error you will immediately see. Sometimes it helps to place a die(); after the var_dump statement so not to end the response before it contains to much other data that will hide the information from you. The devel plugin offers additional debug routines to dump values prominent.
If this is your object:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
I'd guess you can find it using:
field_image->handler->view->result[0]->_field_data['nid'][entity]->field_image['und'][0]['filename]
Could be a mistake in there, but the general Idea is: if you have an object, get the variable using ->, and if you have an array, use [key].
Let's say you have a node object in $node. You can print it's values very nice with:
dpm($node); // remember this function is declared in devel module
Then you can see the information from $node and expand the internal fields with a click. And with a double click on the field you can see it's php path.
You'll get this result:
Hope that helps!
PD: I guess this functionality isn't available on D6's dpm.
Try:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
If you have devel installed and try
krumo ($variable);
Just bear in mind as default only admin users have rights to use the krumo command, but this can be sorted out by looking at the DEVEL role permissions. (don't forget to remove these permissions once your done though)
<? print_r($something["other"]); ?>
(where other is this)
so result is 'this'
Let me summarize up
print_r($data); => Traditional view of printing array.
var_dump($data); => Not so much cleaned view , gives you everything but in very suffocated manner
print "<pre>"; print_r($data); => A cleaned view but will not get data types information.
dpm($data); => It gives you everything, but you need to have installed devel.
You should use field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) , that will return renderable array . You can check api document https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_view_field/7.x
If you can't use devel module for some reason, another useful "debug" functions could be var_export() and a Drupal wrapper drupal_var_export(). These functions gives the output as PHP code.

PHP find needle in array stack [duplicate]

I googled, installed Devel, Drupal for Firebug, but I can't find it.
I found what I want, I know where it is; I just don't know how to get it.
I'll put this in code brackets, but Devel tells me the file name (which I want to stick into the .tpl.php file) is here:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
So, how do I get that FILENAME.jpg to be output using PHP?
<?php print $something->other; ?>
Whenever you need to read a value out of a variable, you need to know which expression you need to formulate to access that value.
For a simple variable value this is simple, you just take the variable name and access it as a variable by prefixing it with the $ sign:
var_dump($variable);
This is documented here.
However this does only work for simple datatypes like string or integer. There are as well compound datatypes, namely array and object. They can contain further datatypes, be it simple or compound. You can learn in the PHP manual how to access the values of an array and how you can access them from an object. I think you already know of that a bit, so just for having it linked here.
When you have learned about that, you can then combine this. E.g. if there is an array within an object and therein is a string you would like to get, you need to combine the $ sign and the variable name with the needed accessors, property names and array keys. Then you get your value. The data you have posted shows that you have an object that has some other objects and arrays and in the end you find the variable name.
Some combination example:
var_dump($variable->handler->view[0]->_field_data);
This is based on the data you've provided above. $variable is where you start, -> is used to access object members which need to be named then (like a name for a variable) : handler. As you've seen in your debug output that handler is an object, you need to use again the -> to access the view member of it.
Now view is different because it's an array. You access values of an array by using [] and putting the key in there. The key in my example is a number, 0. And as the value of that array entry is an object again, in the next step you need to use -> again.
You can continue this game until you reach the element that you're interested in. The debug output you already have helps you to write the expression that returns the value. Possibly it is:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
But I can not validate that here on my system in full.
However when finding things out, it's helpful to make use of var_dump as you could step by step extend the expression until you find the element. If you make an error you will immediately see. Sometimes it helps to place a die(); after the var_dump statement so not to end the response before it contains to much other data that will hide the information from you. The devel plugin offers additional debug routines to dump values prominent.
If this is your object:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
I'd guess you can find it using:
field_image->handler->view->result[0]->_field_data['nid'][entity]->field_image['und'][0]['filename]
Could be a mistake in there, but the general Idea is: if you have an object, get the variable using ->, and if you have an array, use [key].
Let's say you have a node object in $node. You can print it's values very nice with:
dpm($node); // remember this function is declared in devel module
Then you can see the information from $node and expand the internal fields with a click. And with a double click on the field you can see it's php path.
You'll get this result:
Hope that helps!
PD: I guess this functionality isn't available on D6's dpm.
Try:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
If you have devel installed and try
krumo ($variable);
Just bear in mind as default only admin users have rights to use the krumo command, but this can be sorted out by looking at the DEVEL role permissions. (don't forget to remove these permissions once your done though)
<? print_r($something["other"]); ?>
(where other is this)
so result is 'this'
Let me summarize up
print_r($data); => Traditional view of printing array.
var_dump($data); => Not so much cleaned view , gives you everything but in very suffocated manner
print "<pre>"; print_r($data); => A cleaned view but will not get data types information.
dpm($data); => It gives you everything, but you need to have installed devel.
You should use field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) , that will return renderable array . You can check api document https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_view_field/7.x
If you can't use devel module for some reason, another useful "debug" functions could be var_export() and a Drupal wrapper drupal_var_export(). These functions gives the output as PHP code.

ZEND: Appending XML Data to SQLDataBase(XML)

My Issue: Unable to append XML data to prexisting XML data in a MYSQL database.
I have an array - $buyer. Inside this array is a $key and $value similar to (shippingTotal => 55). What I want to do is use something similar to
$param = array(
'shippingTotal' => $shippingTotal
);
$where['quote_data = ?'] = $quoteNumber
$n = $db->update('quote_xml', simplexml_load_string($param), $where);
My hiccup is that the current data inside quote_data is an XML element containing LOTS of information. Is there any way to just "stick" shippingTotal into said existing XML? When I use the above code I just end up with quote_data becoming empty.
I also created a variable called $shippingTotal so that I wouldn't have to use $buyer['shippingTotal']. Still not functional.
Thank you for your time and assistance with this issue.
Aaron
I see a few issues with this:
First, simplexml_load_string doesn't accept array parameters, only XML strings. Since the $params is not a valid argument, it is returning boolean false. Even when successful, it returns a SimpleXMLElement. To convert that to an XML string, you would have to call the asXML() method on the returned object before passing it to Zend_Db_Table::update().
Second, most likely XML cannot just be "appended" to other XML. I don't know exactly what your table holds, but the XML needs to be programmatically added to the existing XML. You can't append XML because the data you want to add needs to be added to the proper nodes.
What you will have to do is first read the value of that column, parse it using SimpleXML, add your new data to the appropriate node in the document using one of the SimpleXML functions and then perform the update.
Hope that helps.

Able to see a variable in print_r()'s output, but not sure how to access it in code

I googled, installed Devel, Drupal for Firebug, but I can't find it.
I found what I want, I know where it is; I just don't know how to get it.
I'll put this in code brackets, but Devel tells me the file name (which I want to stick into the .tpl.php file) is here:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
So, how do I get that FILENAME.jpg to be output using PHP?
<?php print $something->other; ?>
Whenever you need to read a value out of a variable, you need to know which expression you need to formulate to access that value.
For a simple variable value this is simple, you just take the variable name and access it as a variable by prefixing it with the $ sign:
var_dump($variable);
This is documented here.
However this does only work for simple datatypes like string or integer. There are as well compound datatypes, namely array and object. They can contain further datatypes, be it simple or compound. You can learn in the PHP manual how to access the values of an array and how you can access them from an object. I think you already know of that a bit, so just for having it linked here.
When you have learned about that, you can then combine this. E.g. if there is an array within an object and therein is a string you would like to get, you need to combine the $ sign and the variable name with the needed accessors, property names and array keys. Then you get your value. The data you have posted shows that you have an object that has some other objects and arrays and in the end you find the variable name.
Some combination example:
var_dump($variable->handler->view[0]->_field_data);
This is based on the data you've provided above. $variable is where you start, -> is used to access object members which need to be named then (like a name for a variable) : handler. As you've seen in your debug output that handler is an object, you need to use again the -> to access the view member of it.
Now view is different because it's an array. You access values of an array by using [] and putting the key in there. The key in my example is a number, 0. And as the value of that array entry is an object again, in the next step you need to use -> again.
You can continue this game until you reach the element that you're interested in. The debug output you already have helps you to write the expression that returns the value. Possibly it is:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
But I can not validate that here on my system in full.
However when finding things out, it's helpful to make use of var_dump as you could step by step extend the expression until you find the element. If you make an error you will immediately see. Sometimes it helps to place a die(); after the var_dump statement so not to end the response before it contains to much other data that will hide the information from you. The devel plugin offers additional debug routines to dump values prominent.
If this is your object:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
I'd guess you can find it using:
field_image->handler->view->result[0]->_field_data['nid'][entity]->field_image['und'][0]['filename]
Could be a mistake in there, but the general Idea is: if you have an object, get the variable using ->, and if you have an array, use [key].
Let's say you have a node object in $node. You can print it's values very nice with:
dpm($node); // remember this function is declared in devel module
Then you can see the information from $node and expand the internal fields with a click. And with a double click on the field you can see it's php path.
You'll get this result:
Hope that helps!
PD: I guess this functionality isn't available on D6's dpm.
Try:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
If you have devel installed and try
krumo ($variable);
Just bear in mind as default only admin users have rights to use the krumo command, but this can be sorted out by looking at the DEVEL role permissions. (don't forget to remove these permissions once your done though)
<? print_r($something["other"]); ?>
(where other is this)
so result is 'this'
Let me summarize up
print_r($data); => Traditional view of printing array.
var_dump($data); => Not so much cleaned view , gives you everything but in very suffocated manner
print "<pre>"; print_r($data); => A cleaned view but will not get data types information.
dpm($data); => It gives you everything, but you need to have installed devel.
You should use field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) , that will return renderable array . You can check api document https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_view_field/7.x
If you can't use devel module for some reason, another useful "debug" functions could be var_export() and a Drupal wrapper drupal_var_export(). These functions gives the output as PHP code.

getting items from a SimpleXMLElement object

From what I can tell, a SimpleXMLElement is just an array of other SimpleXMLElements, plus some regular array values if there wasn't a tag nested in another tag.
I have a SimpleXMLElement in a variable $data, and var_dump($data) gives me this:
object(SimpleXMLElement)#1 (33) {
["buyer-accepts-marketing"]=>
string(4) "true"
...
...
but calling var_dump($data->buyer-accepts-marketing) gives me an error, and var_dump($data["buyer-accepts-marketing"]) gives me NULL. Calling var_dump($data->shipping-address->children()) gives me an error.
going like this:
foreach($data as $item) {
var_dump($item);
}
gives a whole bunch of SimpleXMLElement objects, but oddly enough, no strings or ints.
What am I missing here? I want to take specific portions of it and pass them to a function, so for example, I don't have to go
$data->billing-address->postal-code;
...
$data->shipping-address->postal-code;
...
and can just go
address($data->billing-address);
address($data->shipping-address);
etc.
SimpleXMLElement is not just an array. To access child elements, you must use object notation ($a->b) and to access attributes you must use array notation ($a['b']).
Problem is, with object notation, valid tag names can be illegal PHP code.
You need to do this:
$data->{'buyer-accepts-marketing'};
Note that this returns a SimpleXMLElement! The reason for this is that it can contain either just text, more child elements, or both. The output of var_dump() is very misleading for SimpleXMLElements. If you want to the text content of a single <buyer-accepts-marketing> tag, you have to do this:
(string)$data->{'buyer-accepts-marketing'};
Of course it is also perfectly legal to do this:
(int)$data->{'buyer-accepts-marketing'};
The reason this appears to work in some cases (such as echoing a SimpleXMLElement) is that the type conversions are implicit and automatic. You can't echo an object, so PHP automatically converts it to a string.
I have a love/hate relationship with SimpleXML. It makes things very easy only after you understand how complex the actual API is.
Read up on the so-called "basic" examples to get a good handle on it.

Categories