How to read value from SimpleXMLElement by using PHP 7.0 [duplicate] - php

This question already has answers here:
SimpleXML Reading node with a hyphenated name
(2 answers)
Closed 4 years ago.
I have an XML with product. I have a problem with read values witch has a "-" in name because this method dosent work:
$productHurtoID = $product->kod-kreskowy->__toString();
Below You have $product variable:
SimpleXMLElement object {
nazwa => SimpleXMLElement object
kod-katalogowy => (string) K0428
kod-kreskowy => (string) 0027084373370
}
Thanks for help. Kind regards

You could access your dynamically created properties via
$productHurtoID = $product->{'kod-kreskowy'}->__toString();
A different approach would be to pre-process your XML input and replace hyphens with camelCase. But depending on your use-case, this might not be possible.
Regards

You can use following code for getting values with - (hyphen) in key.
$productHurtoID = $product->nazwa->{'kod-kreskowy'}->__toString();

Related

Is there a way to escape the "." character in php? [duplicate]

This question already has answers here:
Dealing with special characters in object property names
(1 answer)
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 1 year ago.
I have an stdClass object that I have dynamically created from a JSON using json_decode(). I am trying to access a field by calling $value->V.X->processedField
but this field has a period. This is giving me a syntax error. Is there a way to somehow escape the period in my code, or am I going to have to rename the field in my JSON?
<?php
// example code
$value = json_decode('{"V.X": {"processedField":"the value"}}');
print_r($value->{"V.X"}->processedField);
// or
$var = "V.X";
print_r($value->$var->processedField);

Get variable with PHP from JSON [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I have been scratching my head with this one and wonder if anybody would be kind enough to give me a pointer. I am extracting some data as a variable from JSON to PHP, and I can do this no problem when there are nested nodes - IF the node is a text but not if the node is a number. I am using json_decode
THIS IS NOT THE SAME AS How do I extract data from JSON with PHP?
This is ok
$get_temp = $jsonobj->main->temp;
This is not working
$get_weather = $jsonobj->main->0->weather;
So my question is how do I target the node when it is a number?
Thanks
Probabily you have an array into main node.. so you can get its value with an index like this:
$get_weather = $jsonobj->main[0]->weather;
Where 0 is the index that you want to get
$get_weather = $jsonobj->main[$x]->weather;
$x would be the index
This should work:
$get_weather = $jsonobj->main[0]->weather;

Accessing PHP variable value with : and { in it [duplicate]

This question already has answers here:
What kind of string is this? How do I unserialize this string? [duplicate]
(2 answers)
Closed 7 years ago.
Using Gravity Forms API code GFAPI::get_entries returns an array. Most of the values are straight forward and I have no issues accessing them. However, when it outputs the "List" field it looks like this:
[44] => a:3:{i:0;a:5:{s:7:"Address";s:17:"111 Long St";s:4:"City";s:10:"Southfield";s:5:"State";s:2:"MI";s:3:"Zip";s:5:"48033";s:4:"Type";s:17:"House - Townhouse";}}
How would I set "111 Long St" to a variable? I will need all of the values but I'm sure I can figure it out once I have the answer to getting the Address.
Thanks!
[Edit]
So this is mostly my working code with a few changes to make it easier to read:
$search_criteria["field_filters"][] = array( "key" => "id", value => "10" );
$entries = GFAPI::get_entries( $form_id, $search_criteria );
...
$unserializeArray = unserialize($GLOBALS['entries'][0][44]);
return $unserializeArray[0]["Address"];
http://php.net/manual/en/function.unserialize.php
that's a serialized array in PHP, similar to JSON representation

PHP array value into second array [duplicate]

This question already has answers here:
Forcing a SimpleXML Object to a string, regardless of context
(11 answers)
Closed 9 years ago.
I've been searching around but i'm failing to find an answer to something that seems it should be simple to fix!
I'm reading products from XML and putting their data into an array on a loop, the array is called $res.
Now, i need to put a value from $res into another array for loading to the DB (magento SOAP API). But when i do this i do not get a string value i wxpect, instead i get an the first array inside the second.
Here is the problem line:
$fieldDateData = array('rts_date'=>$res[0]->BackInStockDate1);
I've tried a few different things, none have worked. I thought it would be simply enough to do this:
$data = $res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);
But sadly not, i'm unsure as to why?
Thanks,
EDIT:
This is an example of the output
Array
(
[rts_date] => SimpleXMLElement Object
(
[0] => 28/06/13
)
)
Try
$data = (string)$res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);
You need to cast the value you are setting as a string:
$data = (string) $res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);

reference object with key containing brackets [duplicate]

This question already has answers here:
PHP Object Property has brackets in it
(2 answers)
Closed 9 years ago.
I'm doing a print_r on an object that is returned after running a query. Here is my print_r log.
stdClass Object
(
[MAX(sort_order)] => 3
)
I want to get the value inside [MAX(sort_order)] but I cant figure out how to target it from within php.
Like $sort_order = $object->[MAX(sort_order)]; (I know that won't work)
Does anyone know how I can do this?
Try add this in your query MAX(sort_order) AS max_sort_order in your query

Categories