n8w Posted April 16, 2009 Share Posted April 16, 2009 I am trying to get part of an object into a variable .. but I am not sure how to access it this doesn't seem to work?? print $obj->results['shortUrl']; // 12345 how can I do this? $myvar="http://bit.ly/e4miJ";(I want to extract this part of the object) thanks!!!!!!!!! <? $json = '{ "errorCode": 0, "errorMessage": "", "results": { "http://www.n8w.com/image/art/witchcraft/": { "hash": "IB0iS", "shortKeywordUrl": "", "shortUrl": "http://bit.ly/e4miJ", "userHash": "e4miJ" } }, "statusCode": "OK" }'; $obj = json_decode($json); print $obj->results['shortUrl']; // 12345 ?> Link to comment https://forums.phpfreaks.com/topic/154362-solved-accessing-part-of-an-object/ Share on other sites More sharing options...
n8w Posted April 16, 2009 Author Share Posted April 16, 2009 I know whats in the object .. I just can't seem to figure out the syntax to get the variable I need .. it's an object within an object print $obj->{'errorCode'}; this prints "0" but what I need is print $obj->{'results.child.child.shortUrl'} which should print "http://bit.ly/e4miJ" Link to comment https://forums.phpfreaks.com/topic/154362-solved-accessing-part-of-an-object/#findComment-811596 Share on other sites More sharing options...
Mchl Posted April 16, 2009 Share Posted April 16, 2009 json_decode it first. Too fast on keyboard Try $obj->results->shortUrl; Link to comment https://forums.phpfreaks.com/topic/154362-solved-accessing-part-of-an-object/#findComment-811600 Share on other sites More sharing options...
n8w Posted April 16, 2009 Author Share Posted April 16, 2009 thanks I did .. but I still am not sure how to get this into a variable http://bit.ly/e4miJ object(stdClass)#1 (4) { ["errorCode"]=> int(0) ["errorMessage"]=> string(0) "" ["results"]=> object(stdClass)#2 (1) { ["http://www.n8w.com/image/art/witchcraft/"]=> object(stdClass)#3 (4) { ["hash"]=> string(5) "IB0iS" ["shortKeywordUrl"]=> string(0) "" ["shortUrl"]=> string(19) "http://bit.ly/e4miJ" ["userHash"]=> string(5) "e4miJ" } } ["statusCode"]=> string(2) "OK" } array(4) { ["errorCode"]=> int(0) ["errorMessage"]=> string(0) "" ["results"]=> array(1) { ["http://www.n8w.com/image/art/witchcraft/"]=> array(4) { ["hash"]=> string(5) "IB0iS" ["shortKeywordUrl"]=> string(0) "" ["shortUrl"]=> string(19) "http://bit.ly/e4miJ" ["userHash"]=> string(5) "e4miJ" } } ["statusCode"]=> string(2) "OK" } Link to comment https://forums.phpfreaks.com/topic/154362-solved-accessing-part-of-an-object/#findComment-811602 Share on other sites More sharing options...
Zane Posted April 16, 2009 Share Posted April 16, 2009 I know whats in the object .. I just can't seem to figure out the syntax to get the variable I need .. it's an object within an object print $obj->{'errorCode'}; this prints "0" but what I need is print $obj->{'results.child.child.shortUrl'} which should print "http://bit.ly/e4miJ" results is most likely an array so print $obj->[results][0]['shortUrl']; NEVERMIND Link to comment https://forums.phpfreaks.com/topic/154362-solved-accessing-part-of-an-object/#findComment-811605 Share on other sites More sharing options...
premiso Posted April 16, 2009 Share Posted April 16, 2009 <?php $json = '{ "errorCode": 0, "errorMessage": "", "results": { "http://www.n8w.com/image/art/witchcraft/": { "hash": "IB0iS", "shortKeywordUrl": "", "shortUrl": "http://bit.ly/e4miJ", "userHash": "e4miJ" } }, "statusCode": "OK" }'; $obj = json_decode($json); echo $obj->results->{"http://www.n8w.com/image/art/witchcraft/"}->shortUrl . "<br />"; echo "<pre>"; print_r($obj); die(); ?> If you did a dump of the object you would notice that it listed under the actual url: Output from the above: http://bit.ly/e4miJ stdClass Object ( [errorCode] => 0 [errorMessage] => [results] => stdClass Object ( [http://www.n8w.com/image/art/witchcraft/] => stdClass Object ( [hash] => IB0iS [shortKeywordUrl] => [shortUrl] => http://bit.ly/e4miJ [userHash] => e4miJ ) ) [statusCode] => OK ) Link to comment https://forums.phpfreaks.com/topic/154362-solved-accessing-part-of-an-object/#findComment-811606 Share on other sites More sharing options...
n8w Posted April 16, 2009 Author Share Posted April 16, 2009 awesome!!!!!! works like a charm .. thanks so much premiso Link to comment https://forums.phpfreaks.com/topic/154362-solved-accessing-part-of-an-object/#findComment-811608 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.