How do I get my values out of their SimpleXMLObjects? - php

After an xPath, I'm left with this var_dump:
array(1) {
[0]=>
object(SimpleXMLElement)#2 (1) {
[0]=>
string(11) "22-99586795"
}
}
echoing the damn thing only gives me "Array()"
How do I get the bloody string out?
Thanks

It's an array with one item, so you need to do:
$myelement[0];
or
$myelement[0][0];
(I can't tell from your question which element you're referring to)

Try casting it to string
print (string)$yourarray[0];

Related

Array to string conversion error PHP

array(2) {
[0]=> object(stdClass)#460 (1) { ["deskripsi"]=> string(14) "Embedded teeth" }
[1]=> object(stdClass)#461 (1) { ["deskripsi"]=> string(14) "Impacted teeth" }
}
That is my array result after I vardump from database query.
Then I want to get text Embedded teeth and Impacted teeth
Can someone please help me?
You have an array of objects, the code below should help point you in the right direction
$objects = $myObj;
foreach($objects as $object){
echo $object->deskripsi;
}

How can I extract the value from this SimpleXml?

How can I extract the value from this SimpleXml? I keep getting an empty array, and I don't know what I'm doing wrong.
I just want to extract the string "Familial GI Stromal Tumor With Loss of Heterozygosity and Amplification of Mutant KIT.".
object(SimpleXMLElement)#13 (2) {
["#attributes"]=>
array(2) {
["Name"]=>
string(5) "Title"
["Type"]=>
string(6) "String"
}
[0]=>
string(86) "Familial GI Stromal Tumor With Loss of Heterozygosity and Amplification of Mutant KIT."
}
SimpleXMLElement has a __toString() method. For the element you showed in your question, you should be able to just echo the string content.
echo $yourElement;
or if you want it in a variable, you can call __toString() explicitly
$someVar = $yourElement->__toString();
or trigger it by treating the element as a string;
$someVar = "$yourElement";
Just convert it into a string.
var_dump((string) $element);
Anyway, it depends on your xpath.
Thanks for the help, everyone. Turns out I figured it out. I simply had to access it from the array.
$title = $pub->xpath('Item[#Name="Title"]')[0];

How do I get the values out of the #attribute section?

I cannot figure out to get these values from this array. I need to know the code and name so my application knows which one to go for but I can't get the values out of there. Can someone please help me out? It's the values in the #attributes.
I'm using PHP by the way.
Thanks
array(2) {
[0]=>
object(SimpleXMLElement)#22 (2) {
["#attributes"]=>
array(2) {
["code"]=>
string(3) "HCD"
["name"]=>
string(31) "HIGH COST DELIVERY REGION SRCHG"
}
[0]=>
string(5) "71.25"
}
[1]=>
object(SimpleXMLElement)#24 (2) {
["#attributes"]=>
array(2) {
["code"]=>
string(3) "HCD"
["name"]=>
string(31) "HIGH COST DELIVERY REGION SRCHG"
}
[0]=>
string(5) "71.25"
}
}
Using SimpleXML, you're able to access the attributes on an XML by using the elements as if it was an array ($xml->elementName['attributeName'], or using the ->attributes() method as previously given).
For example, given the following code:
$xml = new SimpleXMLElement('<root><item foo="bar"></item></root>');
If I wanted to access the foo attribute on the item element, I would access it like this:
echo $xml->item['foo'];
However, there's a catch: the value that is returned is actually an instance of SimpleXMLElement. To be able to store or use it, you will need to convert it to a primitive type:
echo (string)$xml->item['foo']; // now a string
echo (int)$xml->item['foo']; // now an integer
echo (bool)$xml->item['foo']; // now a boolean
I figured it out on my own.
$xmlResponse->AccessorialCharges->OtherAccessorialCharges[$i]['code'];

Foreach through an array with objects and return the values and keys

array(14) {
[0]=>
object(stdClass)#2 (2) {
["set_id"]=>
int(44)
["name"]=>
string(7) "Cameras"
}
[1]=>
object(stdClass)#3 (2) {
["set_id"]=>
int(38)
["name"]=>
string(11) "Cell Phones"
}
[2]=>
object(stdClass)#4 (2) {
["set_id"]=>
int(39)
["name"]=>
string(8) "Computer"
}
The Above is my Data.
I want to return the object names ["Set_ID"] etc and the value.
I have googled and googled and tried examples from here with various failures and now I'm giving up.
I have tried to simply manually return data - ie
foreach($result as $results2)
{
foreach($results2->name as $item)
{
echo "<pre>";
print_r($item);
echo "</pre>";
}
}
I have tried all sorts of flavors of it I had kind of hoped the above would at least return data and it didn't - just errored.
In the end, I'd like to be able to both pull names and data. I don't want to have to manually find element names and code it as $result->SET_ID or whatever - I want to be able to just feed SET_ID in as a variable. I think my problem lies with it being an array of objects and I cant access that object name and all..
Im a noob, so any kind of like detailed explanation wouldn't hurt my feelings so I can learn something.
Instead of foreach($results2->name as $item) use foreach($results2 as $item). $results2->name is not an array thus causing the error.
You can read about object iteration in PHP here
foreach($result as $results2)
{
echo $results2->name.' '.$results2->set_id;
}

Get elements of json_encoded array in javascript

i passed a json_encoded array to javascript. Now i would like to acces that array to get the different elemtnts.
i print it out in the console.log() and i get this array:
array(1) {
[16]=>
array(2) {
[3488]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "999184"
["article_name_internal"]=>
string(29) "Geschenkbox Kerzenschein 2011"
}
}
[2615]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "700469"
["article_name_internal"]=>
string(29) "Hotelscheck RomantischeTagef2"
}
}
}
}
This is about right. How can i access the article_name of the second array, with the ID 2615?
found a related question here reading a jsone object, hope for some better explebation or answer. Thanks.
EDIT:
As it seems i made a mistake, i showed a php var_dump in the console. When i try to show the javascript array in the console i get undefined.
Since JSON means "JavaScript Object Notation" you don't need to do anything to access the object's items.
For example you can access:
jsonObject[2615][0]["article_name_internal"]
if this object is String, use eval to convert the string to a JavaScript object and access the items in the same way with the previous example.
var jsonObject = eval(jsonstring);
jsonObject[2615][0]["article_name_internal"]

Categories