Returning a single value of an Associative Array

Discussion in 'Programming' started by tlshaheen, Apr 27, 2010.

  1. #1
    I'm trying to return a single thumbnail from the following array. Right now, foreach loops through the entire array, giving me 4 thumbnails - I only want the first one.

    The original code:
    $videoThumbnails = $videoEntry->getVideoThumbnails();
      foreach($videoThumbnails as $videoThumbnail) {
        echo "<img src='".$videoThumbnail['url']."' height='".$videoThumbnail['height']."' width='".$videoThumbnail['width']."' /> \n";
      }
    Code (markup):
    If I try the following, I get nothing in response.
      $videoThumbnails = $videoEntry->getVideoThumbnails();
      echo "<img src='".$videoThumbnails['url']."' height='".$videoThumbnails['height']."' width='".$videoThumbnails['width']."' /> \n";
    Code (markup):
    Can anyone provide insight? Thank you!
     
    tlshaheen, Apr 27, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    It sounds like its an array of hashes given your use of foreach. So.

    $videoThumbnails = $videoEntry->getVideoThumbnails();
    echo  "<img src='".$videoThumbnail[0]['url']."' height='".$videoThumbnail[0]['height']."' width='".$videoThumbnail[0]['width']."' /> \n";
    PHP:
     
    lukeg32, Apr 27, 2010 IP
    tlshaheen likes this.
  3. tlshaheen

    tlshaheen Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Perfect, thanks! I had tried ['url'][0] previously, and that hadn't worked, but didn't try putting the number first. Thanks!
     
    tlshaheen, Apr 27, 2010 IP