www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to iterate an object

  1. #1
    Join Date
    Jan 2012
    Posts
    12

    How to iterate an object

    Hi - I've consumed some data from a web service and I have it in an object. I'm used to iterating arrays, but I'm having trouble seeing how this works for objects. Here is what the object look like when I do print_r() (I added some formatting for clarity):

    PHP Code:
    stdClass Object 
        [return] => Array ( 
            [
    0] => stdClass Object 
                [
    individualWinePageURL] => 
                [
    previousTradePercentage] => 6.6364 
                
    [region] => bdxr 
                
    [tradePrice] => 1100 
                
    [transactionDate] => 2012-10-17T00:00:00.000+01:00 
                
    [unitSize] => 12x75cl 
                
    [vintage] => 1988 
                
    [wineCode] => 144 
                
    [wineName] => Lynch Bages 
            [
    1] => stdClass Object 
                [
    individualWinePageURL] => 
                [
    previousTradePercentage] => 4.3478 
                
    [region] => rho 
                
    [tradePrice] => 575 
                
    [transactionDate] => 2012-10-16T00:00:00.000+01:00 
                
    [unitSize] => 12x75cl 
                
    [vintage] => 2010 
                
    [wineCode] => 317466 
                
    [wineName] => Clos PapesCNDP 
            [
    2] => stdClass Object 
                [
    individualWinePageURL] => 
                [
    previousTradePercentage] => -1.0417 
                
    [region] => bdxr 
                
    [tradePrice] => 4800 
                
    [transactionDate] => 2012-10-16T00:00:00.000+01:00 
                
    [unitSize] => 12x75cl 
                
    [vintage] => 2004 
                
    [wineCode] => 622 
                
    [wineName] => Lafite Rothschild 
             
    ) ) ) 
    I've tried various kinds of loops but none of them work. Like for example (after loading the object to $results):

    PHP Code:
    foreach ( $results as $key => $value ) {

    echo 
    $results->wineName


    Many variations on this as I looked for examples online as well. The thing that I'm not getting is do I have to name each level of the object? ( stdClass Object - Array - stdClass Object - Key/value?) But if I do, I don't get how to name each part. Please help! :0)

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,226
    Looks like you could do:
    PHP Code:
    foreach($results->return as $data) {
      echo 
    $data->wineName;

    However, a better question might be to look at how you're ending up with a bunch of stdClass objects, when maybe an array would be more appropriate, such as if the data is the result of a json_decode() and you might want to set its optional 2nd parameter to true.
    "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
    Jan 2012
    Posts
    12
    That did it! Thanks for the response.

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