SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2010
    Location
    ONS
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getting an array in <td> one after another

    I do have a little problem in creating table from this dimentional array.

    I am getting dynamic array from one page like,

    Array
    (
    [0] => Array
    (
    [0] => 1374
    [id] => 1374 )
    [1] => 223
    [version_id] => 223

    [1] => Array
    (
    [0] => 1540
    [id] => 1540)
    [1] => 224
    [version_id] => 224

    [2] => Array
    (
    [0] => 1541
    [id] => 1541)
    [1] => 225
    [version_id] => 225
    )

    I want data in table like
    i7ZZK.png
    I love to help and also to get helped.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,412
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    And what is the problem you're having?

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2010
    Location
    ONS
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have written code

    $n = count($multidim_arr);
    $z = count($multidim_arr[$n-1])/2;
    $keys = array('ID', 'version_id');
    echo "<table>";
    for($j = 0; $j<$z;$j++) {
    echo "<tr>";
    for($i = 0; $i<$n; $i++) {
    echo "<td>".$keys[$j]." and value: ".$multidim_arr[$i][$keys[$j]]."</td>";
    }
    echo "</tr>";
    }
    echo "</table>";

    but its printing "<tr><td>ID and value: </td><td>ID and value: </td><td>ID and value: </td></tr>".

    But i want, <tr><td>ID </td><td>value1</td><td>value2</td><td>value3</td></tr>

    I want to print the field_name one time in <td> and then the records, just like the comparison between 3 items.
    I love to help and also to get helped.

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Try the code below and see if this is what you are looking for:
    Code:
    <?php
       $multidim_arr = array(array("First",1374,223),array("Second",1540,224),array("Third",1541,225));
       echo "<pre>";print_R($multidim_arr);echo "</pre>";
    
       $keys = array('Name', 'ID', 'version_id');
       
       $rows = count($keys);
       $cols = count($multidim_arr);
    
       echo '<table border="1">';
       for($j = 0; $j<$rows;$j++)
       {
    	   echo "<tr><td>$keys[$j]</td>";
    	   
    	   for($i = 0; $i<$cols; $i++)
    	   {
    		   echo "<td>".$multidim_arr[$i][$j]."</td>";
    	   }
    	   echo "</tr>";
       }
       echo "</table>";
    ?>

  5. #5
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    BTW, if you are getting the array as you posted then you need this code:

    Code:
    <?php
       $multidim_arr = array(array("id"=>1374,"version_id"=>223),array("id"=>1540,"version_id"=>224),array("id"=>1541,"version_id"=>225));
       echo "<pre>";print_R($multidim_arr);echo "</pre>";
    
       $keys = array('id', 'version_id');
       
       $rows = count($keys);
       $cols = count($multidim_arr);
    
       echo '<table border="1">';
       for($j = 0; $j<$rows;$j++)
       {
    	   echo "<tr><td>$keys[$j]</td>";
    	   
    	   for($i = 0; $i<$cols; $i++)
    	   {
    		   echo "<td>".$multidim_arr[$i][$keys[$j]]."</td>";
    	   }
    	   echo "</tr>";
       }
       echo "</table>";
    ?>


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
  •