Jump to content

Recommended Posts

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

 

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"

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" }

 

 

 

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.