foreach and arrays issue

Discussion in 'PHP' started by nickharper, Feb 12, 2009.

  1. #1
    I have vardumped my array which comes out as this:

    array(2) { [0]=>  object(stdClass)
    
    #308 (5) { ["iId"]=>  int(8) ["sName"]=>  string(14) "Portable Audio" ["iParentId"]=>  int(4) ["sDescription"]=>  string(14) "Portable Audio" ["bAdult"]=>  bool(false) } [1]=>  object(stdClass)
    
    #309 (5) { ["iId"]=>  int(9) ["sName"]=>  string(11) "Televisions" ["iParentId"]=>  int(4) ["sDescription"]=>  string(11) "Televisions" ["bAdult"]=>  bool(false) } } 
    Code (markup):
    What I am basically trying to do is just display the sNames and I have the following code:

    foreach ($array as $value) { if (isset($value->sName)) { echo $value->sName; } }
    PHP:
    It doesnt seem to work though :( Can anybody help?
     
    nickharper, Feb 12, 2009 IP
  2. buldozerceto

    buldozerceto Active Member

    Messages:
    1,137
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    88
    #2
    Is it array of objects?
     
    buldozerceto, Feb 12, 2009 IP
  3. nickharper

    nickharper Active Member

    Messages:
    732
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    75
    #3
    Yes, it is from an API and basically kicks all of that info out per category.
     
    nickharper, Feb 12, 2009 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    Try debug it.

    foreach ($array as $value) print_r($value);
    PHP:
     
    Kaizoku, Feb 13, 2009 IP
  5. bhagwat_banna

    bhagwat_banna Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    is this an array ????

    for this u can also use var_dump(varname);
     
    bhagwat_banna, Feb 13, 2009 IP
  6. nickharper

    nickharper Active Member

    Messages:
    732
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    75
    #6
    When I do a vardump I get what I quoted in the top post :)
     
    nickharper, Feb 13, 2009 IP
  7. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #7
    Interesting situation. Try this:
    foreach ($array as &$value) 
    { if (isset($value->sName)) { echo $value->sName; } }
    PHP:
     
    wmtips, Feb 13, 2009 IP