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.
Related
This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 6 years ago.
I have a webservice that returns a very large json object. I decode this to an array for parsing.
What I'm trying to do though is extract all array members from the json array where there is [id] => 21 at the start of the individual array object.
A nice simple task using LINQ, but not an idea how to do this in PHP. There has to be a way to filter through and extract.
I'd use something like array_filter for this
function find_id($var)
{
// returns whether the is is 21
return($var['id'] === 21);
}
$result = array_filter($jsondata, "find_id"));
array_filter Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.
This question already has answers here:
How to get last key in an array?
(18 answers)
Closed 8 years ago.
As I read though how to get the last value of multidimensional array, end(array) has come up multiples times.
My problem is similar, I have an array like this:
array = (
[12] => Array (xxx => xxx),
[34] => Array (xxx => xxx),
[56] => Array (yyy => yyy)
);
I want to get the index number. If I use end(array) I will get the whole array indexed from [56]. How do I get [56] itself instead of the array?
P.S. I know I can use loop to get the last index number, I just don't want to loop though the whole array to just get the last index number...
$keys = array_keys($yourArray);
$lastKey = $keys[count($keys)-1];
So, get the keys and pick the last one, does this suit you?
I wouldn't recommend this on very large arrays though, if you are doing an iterative operation. I believe the array_keys actually loops the array internally (confirm me on this please).
Alternatively, as #Ghost mentioned in a comment, you can point the array to end with end() and use key() on it to get the key (this is more performant):
end($yourArray);
$lastKey = key($yourArray);
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);
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
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.