Basic PHP - Getting Data Out of an stdClass Object - php

I have a quick question about something I imagine must be pretty easy - I've done a little research and found some links that seem promising, especially this, but it doesn't work for me for some reason.
Anyway, I made a stored procedure in MySQL and tested it with MySQL Workbench, and it works - it just adds num1 and num2 and returns the result. Now I'm trying to get it to work in PHP, but the result, instead of being an integer, is an array of one stdClass Object which contains that integer. That makes sense from the point of view of procedures that return a lot of data, but I'm having some trouble getting down to just the integer.
I run this:
CALL database.routine(2,7)
And I save the results into $var. When I run print_r($var), I get:
Array
(
[0] => stdClass Object
(
[num1+num2] => 9
)
)
So, to get past the Array part, I specifically asked for the first element in it, by running print_r($var[0]), which gets me:
stdClass Object
(
[num1+num2] => 9
)
And now I need to go one level deeper...I tried what the page I linked to above said and attempted to get to $var[0]->[num1+num2], as the field appears to be named, but that doesn't work. I've also tried a few combinations of single quotes and double quotes, but no luck. How do I get the number 9 out of this object?

Try...
$var[0]->{'num1+num2'}

Try this
$var[0]->{"num1+num2"}
or
$prop = 'num1+num2';
$var[0]->$prop;

Did you try this?
$var[0]->{'num1+num2'}

Try the following $var[0]->{'num1+num2'}

This is crazy but it might just work!
$var[0]->{'num1+num2'}

Related

don't know why but somehow substr() doesn't work in code??(php)

recently I'm developing project via PHP, and today I faced a ridiculous situation, and couldn't get the reason still. So here's my problem
let's suppose that I have a string like this : 'myid(myname)'
what I wanted to get was myid and myname separately, so first I tried it with explode(), and I got result like below:
$order_identity= myid(myname);
$idexplode=explode("(", $order_identity);
$idexplode= Array ( [0] => myid [1] => myname) )
It was good, but not satisfied, because I still have ')' after 'myname' so to get rid of ')'
I used substr() which is very basic built-in php method to slice string value.
$order_name = substr($idexplode[1],0,-1);
and I thought I've done quite good and logged it,However, contrary to my expectations.
I still got 'myname)' when I echoed "echo $order_name"; !!!
I never met problem like this before and do not know why this happened to me :/.
can you tell me what's going on here and how to solve my problem?
thx for reading, your help will be appreciated.
p.s I already checked type and it was string! and also I checked if my php sever being lagged but it wasn't!!
use this $order_name - rtrim($idexplode[1],')') Instead of substr()....you get what your want.....hope you got the answer....

Get specific value of array object

I have an array object below
33 => 'a:2:{
s :8:"latitude";
s:10:"39.3600586";
s:9:"longitude";
s:18:"-84.30993899999999";
}'
And here is the variable I'm using to get the value of that object
$events_location = $entries[1]['33'];
Is there a way to get just the value of either the latitude or longitude instead of everything in the single quotes?
Thanks!
What you have here is a serialized string. Unserialize it to access the key in the array:
$events_location = unserialize($entries[1]['33']);
echo $events_location['longitude'];
This should be a comment but its a bit long.
The string you have shown us looks vaguely like part of a serialized php entity. But it's not. If you try to unserialize this you'll get an error. The underlying data appears to be a coordinate - but even ignoring the syntactic errors, the semantics of the structure are wrong.
Please check the source you transcribed this from. If you have not copied the original content here please amend your question.
If you have transcibed it correctly then go speak to whoever supplied you with this data and ask them to fix it.

Cannot access PHP Object as Array

I am facing a strange problem. It may just be a silly mistake, and I am just missing some basics.
I'm running php 5.6.1 on MAMP.
I have a simple array which I get from a mysql query. Using a foreach loop, I can print_r() each value, which gives me: stdClass Object ( [srno] => 6 [link] => this-is-link )
Now I can echo $obj->srno, and that prints fine. But I can't use echo $obj['srno'] which I was previously using, on an older version of PHP, but- It shows nothing.
Any help really appreciated. Thanks!
If you have a stdClass object and need to address it as an array, you can cast it to array quite easily:
$someObj = new stdClass();
$someObj->foo = "bar";
$someArray = (array)$someObj; // Cast the object to an array
echo $someArray['foo']; // Will give you "bar"
Working example: http://3v4l.org/nni1Y
Of course as comments already pointed out, you may want to look at retrieving your mysql results as an array in the first place.
As you said your results return as an object so you can use it by using
$obj->your_field_name to display field value. But your can not use by $obj['field_name'];

How to retrieve data from # xml attribute in PHP

Ok so I am stuck with this xml in PHP stuff. I have gotten pretty far considering its my first 3 hours into XML all together ever in my entire life.
I am having trouble pulling data from a XML thing that has # in the name. See below (obviously im not going to post the whole XML thing but u can see how i got there below that.
SimpleXMLElement Object
(
[#attributes] => Array
(
[date] => 2010-09
[reserved] => 6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
)
)
How i got there:
echo $this->General_functions->naked($xml->property[0]->availability->month[0]);
General_functions->naked is just a fast function to wrap and print_r around the given attribute.
My question is, HOW do i get the values inside #attributes cause no matter what i try i cant figure it out. Ive searched the web for a good 45 mins with no real answer.
Thanks in advance.
David
You need to use the attributes() method to get the results as another class. So, for example, to get the date attribute:
$myElement->attributes()->date
Also note that it's not a string, it's a SimpleXML attribute. If you want to get its actual value, you need to cast it to string explicitly:
(string)$myElement->attributes()->date
Access attributes of an element just as you would elements of an array:
(string) $xml->property[0]->availability->month[0]['date']
Edited to add the cast.

Array call from key not working correctly

I'm trying to call to a specific part of an array with the key # and it's not working. I can output the array and see it...
Array
(
[6] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
[7] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
[8] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
)
This array is $emailDB. I can call to the array manually with $emailDB[7] and it works, but if my call is dynamic like this it won't work...
<?php
$value = 7;
print_r($emailDB[$value]);
?>
I've never had an issue like this with an array so it's very odd. What really sucks is I'm under deadline with a form not working on a client's site...joy.
We tried this with no luck...
<?php
$value = 7;
print_r($emailDB[intval($value)]);
?>
I thought intval() would assist but it did not.
You're post implies a bug in php itself, which I highly doubt. What's more likely is that what you posted doesn't properly represent the code you're running.
Why don't try this. Make a brand new empty php file. Hardcode the array keys and values and assign them to the $emailDB variable, and then try
$value = 7;
print_r($emailDB[$value]);
You will see you don't have the problem that you claim. You have now started the debugging process, and now you can look at the working, and non working code to compare the difference.
Well, you are echoing an Array, which I assume is printing "Array" onto your screen. If you want to echo the actual contents of the array, you need to use print_r($array) or echo print_r($array, true). You can also try putting the value in quotes, like $emailDB["{$value}"] to see if that works, I sometimes have troubles with integers not going into things properly.
I agree with you all. It had to have been something whacky with how we were pulling in the data somehow. It was a tab-separated file we were exploding. I just re-wrote the whole thing entirely and imported the data into MySQL and all was well.
In hindsight, I have a sneaking suspicion it was a trim() command that was needed and likely nothing more. Dang it...too late, but I learned something about checking over the code for those types of things.

Categories