简体   繁体   中英

Multidimensional array to table or split multidimensional arrays

Sad to say i have a little problem again :-(

I have a multidimensional array from a database now i want to write it in a table where the groupe value should be the column header and all records with the same groupe value should be in the rows of the column.

The array is ordered by the groups.

Here my test array the original has 8 values in the sub array but i think it will show the problem:

Array ( 
    [0] => Array 
        ( [0] => id [1] => name1 [2] => mail1 [3] => groupe1)

    [1] => Array 
        ( [0] => id2 [1] => name3 [2] => mail3 [3] => groupe1)

    [2] => Array 
        ( [0] => id3 [1] => name2 [2] => mail2 [3] => groupe2)
) 

The table look like:

<table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td>Groupe 1</td>
    <td>Groupe 2</td>
  </tr>
  <tr>
    <td>name1</td>
    <td>name2</td>
  </tr>
  <tr>
    <td>name3</td>

  </tr>
</table>

I think the solution will be to strip down the groupes to a one dimensional array remove duplicates and loop through the header.

Then split the array by groupes and loop through the rows ...

But what ever i try i fail by modeling the new arrays ... and maybe you know a easier way :)

Thank you very much !

It seems you are getting a common problem, you want to display your data in columns instead of rows. The solution is to use an intermediate array in which you store your data by group.

So, loop on your rows, get the group, if the group does not exist in array, create array. By the way you want to calculate the maximum number of row in a group. The easier way is to loop on your group array and to use count and >. Use the FOR loop to reach max value, if a group has no data for this index (use isset()), display and empty string, else display the name (or the data you want).

You could do it with 2-3 loops... :D

Bonus: You can optimize this algorithm by using ORDER BY group_id in your SQL query, then you could get the max length directly in the first loop..

Got it :-)

I think this solution is may be not the best but it works and can be used for similar situations.

What i have done:

  1. Get only the groupe values from the data array and make it unique.

  2. Split the Data array in separate groupe arrays

  3. Build the table with a help table for the rows.

Here is a sample code:

<?php
//Make test array Data;
$data = array();
$data[0][0] = 'Name1';
$data[0][1] = 'GroupeA';

$data[1][0] = 'Name2';
$data[1][1] = 'GroupeA';

$data[2][0] = 'Name3';
$data[2][1] = 'GroupeB';

print_r ($data);
echo ('</br>');
echo ('</br>');

//Get only all groupes from the data array and make it unique so every groupe is shown only once
//For the header and for counting.

$groups1 = array();
$groups2 = array();
$x = 0;
foreach($data as $value) {
$groups1[$x] = $value['1'];
$x ++;
}

$groups2 = array_unique($groups1); //Make unique so every groupe is shown only once in the array
$groups = array_values($groups2); // Renumber the Array Keys ( 0 to ?)

//Split the data array to seperate groupe arrays and give them auto names.
//So every Groupe has its own array called grpdata0 to grpdata?

$x = 0;
$x2 = 1;
$gr = count($groups);


while($gr>0) {

foreach($data as $value) {
if ($value[1] == $groups[$x]) {
${'grpdata' . $x}[$x2] = $value;
$x2++;
}
}

$gr--;
$x++;

}

// Renumber the array keys from the grpdata arrays ( 0 to ?)
$gr2 = count($groups)-1;
while($gr2>=0) {
${'grpdata' . $gr2} = array_values(${'grpdata' . $gr2});
$gr2--;
}

//Make the table
?>

<table border="1" cellspacing="0" cellpadding="0">
  <tr>

<?php //Make the header depending on the groups
  foreach($groups as $value) {
  echo ('<td><center>' . $value . '</center></td>');
  }?>

    </tr>
  <tr>

<?php // Make the columns (by groupe) and add the rows (with a helping table)
 $x = 0;
  foreach($groups as $value) { 
  echo ('<td valign="top">');
  foreach(${'grpdata' . $x} as $value) { ?>

  <table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center"><?php echo $value[0]; ?></td>
  </tr>
</table>

  <?php
  }
  echo ('</td>');
  $x++;
  } ?>

</tr>

</table>

Thank you !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM