PHP array value into second array [duplicate] - php

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);

Related

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

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();

Fetching data from array inside array [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
I've finally figured out how to fetch data from Google Analytics to website, unfortunately output (print_r) is PHP array inside an array:
Array ( [0] => Array ( [0] => 196 ) )
I need to get the number 196 & "save" it as variable in order to use it in calculations or output it, any ideas how to achieve this?
Im looking for overall solution/principle/rule because some data is even array inside array inside array etc.
you need to grab the first array element -position 0 - and from this inner array you pick the first element - also position 0 - which is 196, Check this: PHP Fiddle - hit run to execute
// getting 196
echo $myArr[0][0];
Use this:
$newVariable = $yourArray[0][0];
echo $newVariable; //This will echo the answer.
//This answer is self explanatory.

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

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

Is it possible to use PHP's [ ] array vars in a GET request? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to pass an array via $_GET in php?
So I have a script which receives a $_POST with var Board[] containing an array of IDs.
I realized when adding ShareThis to the pages that folks wanting to share the result set would send an invalid link.
I definitely can add a delimited parameter to the script so ShareThis can pass
http://foo.com/bar.php?DelimitedBoard=3|4|5.
My question is whether there is a way to do this using the current Board[] var?
http://foo.com/bar.php?Board[]=3,4,5 fails. Is there a way?
Sure, just do:
http://foo.com/bar.php?Board[]=3&Board[]=4&Board[]=5
What you want to do is something like this:
http://foo.com/bar.php?Board[]=3&Board[]=4&Board[]=5
Then, if you print_r($_GET['Board']); in bar.php, you will get something like:
Array (
[0] => 3
[1] => 4
[2] => 5
)
Number array serialization is very easy. Use join(',', $Board) to form a comma-separated ID string, use explode(',', $_GET["Board"]) to get the array back.

Categories