Newbie PHP: Insert a variable for echoing
I'd like to be able to echo $domain
from this
$domain = $response['results']['$MYVAR']['shortUrl'];
I've tried curly braces and various other ways of formatting $MYVAR but the syntax is wrong.
Help most welcome !
EDIT --> var_dump($response):
object(stdClass)#1 (4) {
["errorCode"]=> int(0)
["errorMessage"]=> string(0) ""
["results"]=> object(stdClass)#2 (1) {
["http://www.domain.com"]=> object(stdClass)#3 (5) {
["userHash"]=> string(6) "oSEMki"
["shortKeywordUrl"]=> string(0) ""
["hash"]=> string(6) "oms2ZB"
["shortCNAMEUrl"]=> string(20) "http://bit.ly/LALALA"
["shortUrl"]=> string(20) "http://bit.ly/LALALA"
}
}
["statusCode"]=> string(2) "OK"
}
I can see the "domain.com" element fine but when I do this:
var_dump($response['results'][$MYVAR]);
it returns NULL ! Which must be why $domain = $response['results开发者_JS百科'][$MYVAR]['shortUrl']; fails too. Odd !
--EDIT 2 --
var_dump($MYVAR);
gives:
string(118) "http://www.domain.com"
Try this:
$domain = $response['results'][$MYVAR]['shortUrl'];
echo $domain;
Are you sure it's stored in a 3 dimentional array like that? Because that looks like needless complication.
Try it without quotes
$domain = $response['results'][$MYVAR]['shortUrl'];
or use double quotes
$domain = $response['results']["$MYVAR"]['shortUrl'];
EDIT:
In reaction to your edit. You are accessing variable like an associative array but the variable is instance of stdObject. So if you want to acces it, you must retype it like this:
$tmp = (array) $response;
$domain = $tmp['results'][$MYVAR]['shortUrl'];
or access it like object
$domain = $tmp->results->$MYWAR->shortUrl;
EDIT 2:
So it is strange, because http://www.domain.com
is not 118 characters long, as var_dump wrote.
Where and how did you filled up the variable $MYVAR
?
$domain = $response['results'][$MYVAR]['shortUrl'];
You don't need quotes around $MYVAR.
Have you tried without the quotes around $MYVAR?
It's an object.
$domain = $response->results->$MYVAR->shortUrl;
精彩评论