0

I am having a multidimensional array with same values like this,

[documents] => Array
(
    [0] => Array
            (
                [doc_id] => 3
                [doc_type] => driving_licence                   
                [expiry_date] => 2015-11-26
                [added_date] => 2015-11-16
            )

    [1] => Array
            (
                [doc_id] => 3
                [doc_type] => driving_licence
                [expiry_date] => 2015-11-26
                [added_date] => 2015-11-16
            )

)

So now I need to echo this array in a html table with single <tr>.

This is how I tried it :

foreach ($userData as $key => $value) {
    $i=0; 
    foreach ($value['documents'] as $k => $v) {
        $i++; 
        $html  = "<tr>\n"; 
        $html .= " <td class='center'>{$i}</td>";
        $html .= " <td>{$v['doc_type']}</td>";
        $html .= " <td>{$v['expiry_date']}</td>";
        $html .= " <td>{$v['added_date']}</td>";
        $html .= "</tr>\n"; 

        echo $html; 
    }
}

But its repeating table <tr> with same values.

Can anybody tell me how to avoid this? Thank you.

user3733831
  • 2,886
  • 9
  • 36
  • 68

2 Answers2

1

As requested, here is an example for you.

$userData = $input = array_map("unserialize", array_unique(array_map("serialize", $userData)));

foreach ($userData as $key => $value) {
    $i=0; 
    foreach ($value['documents'] as $k => $v) {
        $i++; 
        $html  = "<tr>\n"; 
        $html .= " <td class='center'>{$i}</td>";
        $html .= " <td>{$v['doc_type']}</td>";
        $html .= " <td>{$v['expiry_date']}</td>";
        $html .= " <td>{$v['added_date']}</td>";
        $html .= "</tr>\n"; 

        echo $html; 
    }
}

That will give you a table with 1 <tr> row.

Community
  • 1
  • 1
Darren
  • 13,050
  • 4
  • 41
  • 79
  • Copy and paste work for this line `$userData = $input = array_map("unserialize", array_unique(array_map("serialize", $userData))); ` – user3733831 Jan 04 '16 at 02:29
  • I tried it removing `$input` like this `$userData = array_map("unserialize", array_unique(array_map("serialize", $userData)));`. but it doesn't work for me. – user3733831 Jan 04 '16 at 02:31
  • @user3733831 Change it to - `$userData['documents'] = array_map("unserialize", array_unique(array_map("serialize", $userData['documents'])));` – Darren Jan 04 '16 at 02:43
0

this is the way i would do what you are trying to do:

 $movies = array 
   (
      array('Office Space' , 'Comedy' , 'Mike Judge' ),
      array('Matrix' , 'Action' , 'Andy / Larry Wachowski' ),
      array('Lost In Translation' , 'Comedy / Drama' , 'Sofia Coppola' ),
      array('A Beautiful Mind' , 'Drama' , 'Ron Howard' ),
      array('Napoleon Dynamite' , 'Comedy' , 'Jared Hess' )
   );

   echo "<table border=\\"1\\">";
   echo "<tr><th>Movies</th><th>Genre</th><th>Director</th></tr>";
   for ( $row = 0; $row < count($movies); $row++ )
   {
      echo "<tr><td>";
      for ( $column = 0; $column < count($movies); $column++ )
      {
         echo $movies[$row][$column] ;
      }
      echo "<td></tr>";
   }
The Beast
  • 1
  • 1
  • 2
  • 24
  • 2
    using for with count is really primitive, foreach is better – Phiter Jan 04 '16 at 02:31
  • @PhiterFernandes what is your evidence for `for` being more primitive out of curiosity? – Rasclatt Jan 04 '16 at 02:35
  • You don't need to use a counter to retrieve data, also it uses both keys and values easily. Also the `for` described in the answer will check for the size of the array in every loop. Foreach wouldn't do it. – Phiter Jan 04 '16 at 02:36
  • @PhiterFernandes Easier isn't necessarily more advanced, and as described in the answer the `count` can be repositioned before the iteration so it's not being run in every iteration, so I think one could argue easier, but I don't think `for` is primitive/obsolete. I think either iterator is fine depending on the task it's being applied to. – Rasclatt Jan 04 '16 at 02:58
  • To iterate trough strings it's obviously better. For is a good loop of course but not to iterate trough strings. – Phiter Jan 04 '16 at 11:36