www.webdeveloper.com
Results 1 to 5 of 5

Thread: multiple arrays

  1. #1
    Join Date
    Sep 2006
    Posts
    270

    multiple arrays

    Hi everybody !

    I would have a short question : I have 5 arrays (for instance: name,surname, zip, size, group) and I would like to print their content in a table with 5 colums, each colum corresponding to one of the 5 arrays something like
    name -------surname -------zip -----size--------group//1st value of each array
    name -------surname -------zip -----size--------group//2nd value of each array
    name -------surname -------zip -----size--------group//3rd value of each array
    etc...



    How can I do that ?

    thanks a lot !!!!!!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    PHP Code:
    <?php
    $names    
    = array("John""Jim""Jack");
    $surnames = array("Smith""Sullivan""Snyder");
    $zips     = array('12345''23456''34567');
    $sizes    = array('small''medium''large');
    $groups   = array('this''that''other thing');

    echo 
    "<table>\n";
    while
    (
       (list(
    $k1$name)    = each($names))    &&
       (list(
    $k2$surname) = each($surnames)) &&
       (list(
    $k3$zip)     = each($zips))     &&
       (list(
    $k4$size)    = each($sizes))    &&
       (list(
    $k5$group)   = each($groups))
    )
    {
       echo 
    "<tr><td>$name</td><td>$surname</td><td>$zip</td>";
       echo 
    "<td>$size</td><td>$group</td></tr>\n";
    }
    echo 
    "</table>\n";
    ?>
    "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
    Jul 2004
    Location
    Sendai, Japan
    Posts
    1,273
    PHP Code:
    <?php

    $name
    =array('bob','jim','bill');
    $surname=array('jones','beam','posters');
    $size=array('big','average','unknown');

    $set=array($name,$surname,$size);

    echo 
    '
    <table>
    '
    ;
    for (
    $i=0$i<count($set); $i++) {
    echo 
    '
    <tr><td>'
    .$set[0][$i].'</td><td>'.$set[1][$i].'</td><td>'.$set[2][$i].'</td></tr>
    '
    ;
    }
    echo 
    '
    </table>
    '
    ;
    ?>
    [edit]I was thinking the same as NogDog (see below), but I just couldn't be bothered writing it...[/edit]
    Last edited by BonRouge; 10-06-2006 at 07:05 AM.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    And whatever method you like, probably the best solution is to not have 5 logically related arrays, but instead one multi-dimensional array:
    PHP Code:
    $data = array
    (
       array
       (
          
    'name' => 'John',
          
    'surname' => 'Smith',
          
    'zip' => '12345',
          
    'size' => 'small',
          
    'group' => 'this group'
       
    ),
       array
       (
          
    'name' => 'Jane',
          
    'surname' => 'Doe',
          
    'zip' => '01234',
          
    'size' => 'medium',
          
    'group' => 'that group'
       
    )
    ); 
    You could then just do a foreach on it:
    PHP Code:
    foreach($data as $row)
    {
      echo 
    "<tr><td>{$row['name']}</td><td>{$row['surname']}</td>...etc...</tr>\n";

    "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

  5. #5
    Join Date
    Sep 2006
    Posts
    270
    thank you very much to both of you ! I followed your advices and it works great !!!!!

    thanks thanks and thanks !

    patrick

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