0

I have the following data inside one of my arrays :

array (size=3)
  0 => 
    array (size=4)
      'ID' => string '1747' (length=4)
      'OWNER' => string 'ME' (length=7)
      'TABLENAME' => string 'MYFIRSTTABLE' (length=15)
      'COLUMNNAME' => string 'MYFIRSTFIELD' (length=11)
  1 => 
    array (size=4)
      'ID' => string '1756' (length=4)
      'OWNER' => string 'ME' (length=7)
      'TABLENAME' => string 'MYFIRSTTABLE' (length=15)
      'COLUMNNAME' => string 'MYSECONDFIELD' (length=12)
  2 => 
    array (size=4)
      'ID' => string '1757' (length=4)
      'OWNER' => string 'ME' (length=7)
      'TABLENAME' => string 'ANOTHERTABLE' (length=15)
      'COLUMNNAME' => string 'ANOTHERFIELD' (length=16)

So far, everything is OK: I can loop through each rows and print them in a HTML table. Something like this: enter image description here

Now, I was wondering how to produce something like this:

enter image description here

I've tried some nested foreach, but without any success.

Thanks in advance for your help!

2
  • You want the column names in a different row to the table names? Commented Jan 19, 2020 at 9:49
  • Hi Nick, exactly! In fact, for each tablename, I want to retrieve each columnnames. Commented Jan 19, 2020 at 9:54

1 Answer 1

1

I think something like this should do what you want. It finds all the table names from the data (using array_unique on the output of array_column), then finds all the column names for each table (using array_filter, then array_values to re-index the keys of the array). It then loops over the tables and columns, outputting a table of table names and their column names:

$tables = array_unique(array_column($data, 'TABLENAME'));
$columns = array();
foreach ($tables as $tn) {
    $columns[$tn] = array_values(array_filter($data, function ($v) use ($tn) {
        return $v['TABLENAME'] == $tn;
    }));
}
$max_columns = max(array_map(function ($v) { return count($v); }, $columns));
echo "<table>\n";
foreach ($tables as $tn) {
    echo "<tr><td>$tn</td><td colspan=\"$max_columns\"></td></tr>\n";
    echo "<tr><td></td>";
    foreach ($columns[$tn] as $key => $column) {
        echo "<td>{$column['COLUMNNAME']}</td>";
    }
    if ($key < $max_columns - 1) {
        echo "<td colspan=\"" . ($max_columns - $key - 1) . "\"></td>";
    }
    echo "</tr>\n";
}
echo "</table>\n";

Output (for your sample data)

<table>
  <tr>
    <td>MYFIRSTTABLE</td>
    <td colspan="2"></td>
  </tr>
  <tr>
    <td></td>
    <td>MYFIRSTFIELD</td>
    <td>MYSECONDFIELD</td>
  </tr>
  <tr>
    <td>ANOTHERTABLE</td>
    <td colspan="2"></td>
  </tr>
  <tr>
    <td></td>
    <td>ANOTHERFIELD</td>
    <td colspan="1"></td>
  </tr>
</table>

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.