1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

JSON Problem

Discussion in 'PHP' started by ssimon171078, Apr 25, 2015.

  1. #1
    i need to print all elements of object (obj) in php i need to print with echo obj :
    my code is :
    <?php
    $url  = 'http://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48';
    $opts = array('http' => array(
        'header' => 'Cookie: locale=en%3B0%3Bfalse; suggested_locale=1;',
    ));
    $ctx  = stream_context_create($opts);
    $data = file_get_contents($url, false, $ctx);
    $data = gzdecode($data);
    $obj = json_decode($data);
    
    ?>
    PHP:

     
    ssimon171078, Apr 25, 2015 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #2
    you could check to see how your data is structured with

    print_r($obj);
    PHP:
    It should be an array so you can just do $obj['somekey']
     
    Anveto, Apr 25, 2015 IP
  3. O-D-T

    O-D-T Member

    Messages:
    180
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    43
    #3
    O-D-T, Apr 26, 2015 IP
  4. DMEGroup

    DMEGroup Active Member

    Messages:
    358
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    83
    #4
    To print a array as a json object, you should do this:

    echo json_encode($obj);
    PHP:
    It looks like you are doing a json_decode, i don't see where it would already be a json object?
     
    DMEGroup, Apr 26, 2015 IP
  5. Leela Narasimha

    Leela Narasimha Member

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    Data is coming in json encoded format and you are decoding the json to object.

    You cannot use echo for json decoded as it is an object.

    json_decode($data) -> returns Object
    json_decode($data) -> returns array

    if you echo the json decoded output it displays their respective datatype as object or array.

    Better use var_dump or print_r.

    If you want to display elements in object use echo $obj->key; displays the value of that key.
     
    Leela Narasimha, May 5, 2015 IP