SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Looping arrays keys

    Hi I'm doing a listing page for drupal and have 5 of these arrays under $node.

    field_data_sheet_link_1
    field_data_sheet_link_2
    field_data_sheet_link_3
    field_data_sheet_link_4
    field_data_sheet_link_5

    The value I want to get is in:

    $node->field_data_sheet_link_1[0]['view']
    $node->field_data_sheet_link_2[0]['view']
    $node->field_data_sheet_link_3[0]['view']
    $node->field_data_sheet_link_4[0]['view']
    $node->field_data_sheet_link_5[0]['view']

    How do I do this with loops instead of repeating the statements??
    PS: Title should be looping array names.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    for($i 1$i <=5$i++)
    {
      
    $var 'field_data_sheet_link'.$i;
      echo 
    $node->{$var}[0]['view'];

    or

    PHP Code:
    for($i 1$i <=5$i++)
    {
      echo 
    $node->${'field_data_sheet_link'.$i}[0]['view'];

    Whichever you like best

  3. #3
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    PHP Code:
    for($i 1$i <=5$i++)
    {
      
    $var 'field_data_sheet_link'.$i;
      echo 
    $node->{$var}[0]['view'];

    or

    PHP Code:
    for($i 1$i <=5$i++)
    {
      echo 
    $node->${'field_data_sheet_link'.$i}[0]['view'];

    Whichever you like best
    cool...u made it so simple. i had a close one to your first solution. seems to be the same to me. but why doesn't it work?

    Code PHP:
    $var1 = 'field_data_sheet_link_' . $i. '[0]["view"]';
    echo $node->{$var1};

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by resting View Post
    seems to be the same to me. but why doesn't it work?

    Code PHP:
    $var1 = 'field_data_sheet_link_' . $i. '[0]["view"]';
    echo $node->{$var1};
    Because that assumes $node has a property e.g. 'field_data_sheet_link_0[0]["view"]', which it doesn't (and is an invalid variable name to booth).
    It has a property "field_data_sheet_link", which is an array, that has key "0", which also is an array that has a key "view".

  5. #5
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Because that assumes $node has a property e.g. 'field_data_sheet_link_0[0]["view"]', which it doesn't (and is an invalid variable name to booth).
    It has a property "field_data_sheet_link", which is an array, that has key "0", which also is an array that has a key "view".
    ic...tink i got it. thanks! this is certainly useful


Bookmarks

Posting Permissions

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