Accessing SimpleXML Object attribute - php

Have this print output from print_r($theobject);
SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => a
)
[0] => Abnormal psychology : Abnormal psychology :
)
Just cannot find a way to get element 0 which is "Abnormal psychology :"
Lets call the object as $theobject
I did $theobject[0], didn't get anything.
Many Thanks

You can just cast the object to a string:
$str = (string)$theobject;

Related

PHP Get / check value from associative array in array of individual stdClass Objects

Having real issues with this. I want to be able to get a value from this data which is returned via an API.
ie get value by
$CM_user_customfields['Organisation'],
$CM_user_customfields->Organisation.
is that even possible? I have tried loops and rebuilding the array but i always end up with a similar results and perhaps overthinking it.
I can't use the [int] => as the number of custom fields will be changing a lot.
$CM_user_customfields = $CM_details->response->CustomFields ;
echo '<pre>' . print_r( $CM_user_customfields, true ) . '</pre>';
// returns
Array
(
[0] => stdClass Object
(
[Key] => Job Title
[Value] => Designer / developer
)
[1] => stdClass Object
(
[Key] => Organisation
[Value] => Jynk
)
[2] => stdClass Object
(
[Key] => liasoncontact
[Value] => Yes
)
[3] => stdClass Object
...
many thanks, D.
I recommend convert to associative array first:
foreach($CM_user_customfields as $e) {
$arr[$e->Key] = $e->Value;
}
Now you can access it as:
echo $arr['Organisation'];
You can also achieve it by: (PHP 7 can convert stdClass and will do the trick)
$arr = array_combine(array_column($CM_user_customfields, "Key"), array_column($CM_user_customfields, "Value")));

How to get the value from SimpleXMLElement Object which starts with #?

I am stuck in a problem, I want to access a value which comes under the SimpleXMLElement Object under #attributes category, here is the array:
SimpleXMLElement Object (
[#attributes] => Array (
[src] => source/send_smsf.php
[name] => mainFrame
[id] => mainFrame
[title] => mainFrame
)
)
I want to access the id attribute inside it. But how I can do that?
I got the answer :
$id = $mainNode->attributes()->id;
We can place the #attribute in attributes function and then can call any value inside.
You can simply get that object to a variable and access it like this,
$id = (string) $object['id']; //$object is the SimpleXMLElement object.

PHP Node no longer exists SimpleXML

I have a SimpleXMLElement object that I got from parsing a response from oData. I'm looping through the children and it works well as long as it has children (the truth is I'm not even sure if they are its children or if its just a collection). This is how my code looks like:
$dataset = soapService->doSoapCall();
$array = $dataset->children()->children();
foreach($rawArray as $element)
{
$row['Something'] = (string)$element->Something;
$newArr[] = $row;
}
The problem is that this code throws an error of: PHP Node no longer exists SimpleXML. I've been trying to check for null or check the count of it but I keep getting the same error. When I look at it from the debugger in net beans it just says its type SimpleXMLElement but I doesn't have any other value or children. Please help.
Note: When I do print_r($array) i get this:
SimpleXMLElement Object ( )
When the object actually has data it returns this:
SimpleXMLElement Object ( [Table] => Array ( [0] => SimpleXMLElement Object ( [EventCode] => 10020 ) [1] => SimpleXMLElement Object ( [EventCode] => 10030 ) [2] => SimpleXMLElement Object ( [EventCode] => 10040 ) ) ) {"code":99200}

How do I grab the value from this SimpleXMLElement as a string in PHP

Say I have a variable called $myXMLElement and when I dump the variable it looks like this.
SimpleXMLElement Object (
[#attributes] => Array ( [id] => 7 )
[0] => Kirkpatrick
)
How do I grab the value of Kirkpatrick in PHP
Have you tried:
(string) $var[0]
? Normally you do that with string casting, will turn SimpleXMLElements into the XML nodeValue.
This should get the value:
echo $myXMLElement->{'0'};

php SimpleXml & arrays issue

When I print_r($var) I get the result below.
SimpleXMLElement Object
(
[SEND_FILE] => SimpleXMLElement Object
(
[FILEID] => 123
[GUID] => 456
[SUMMARY] => SimpleXMLElement Object
(
[NB_PAYMENTS] => 1
)
)
)
How can I get the value of the FILEID element in a variable? If I do
print $result->SEND_FILE->FILEID[0]
then I just get the number - what I want, no mention of a SimpleXML Object.
But if I put this variable in an array, as such
$res['file_id'] = $result->SEND_FILE->FILEID[0]
and then print_r($res) I get:
Array
(
[file_id] => SimpleXMLElement Object
(
[0] => 307466
)
)
How can I get it to remove the [0] / SimpleXMLElement Object?
This will look not too elegant, but try casting the result to integer (if the type is known):
$res['file_id'] = (int)$result->SEND_FILE->FILEID[0]
Why do you append the [0] at the end? You dont need that. You should simply do
print $result->SEND_FILE->FILEID;
And that should be enough.

Categories