www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to print an element of an object?

  1. #1
    Join Date
    Aug 2011
    Posts
    51

    How to print an element of an object?

    Hi all,

    I have this object:
    PHP Code:
    $time_periods_array json_decode($data_array[$row_number]['project_timeperiods']); 
    Which the print_r produces:
    PHP Code:
    stdClass Object ( [timeperiod1-1] => 1 12:00 AM [timeperiod1-2] => 3 12:30 AM [timeperiod1-3] => A.MPeak Hour [timeperiod_totaltime] => 0.5 
    But it just won't give me any result (error) on:
    PHP Code:
    echo $time_periods_array['timeperiod1-1']; 
    So, how do I print an element from it? Thank you in advance for your help.

    Dhimoet

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,611
    I usually "cheat" and set the optional 2nd parameter for json_decode() to true, so that everything is an array.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2011
    Posts
    51
    Hi NogDog,

    Thank you for the great solution. I had a similar solution using an extra line, such as:
    PHP Code:
    $time_periods_array json_decode($data_array[$row_number]['project_timeperiods']);
    $time_periods_array = (array) $time_periods_array
    I never noticed that json_decode had such parameter to do those 2 lines above into just 1 line of code.

    Dhimoet

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles