簡體   English   中英

多維數組到表或拆分多維數組

[英]Multidimensional array to table or split multidimensional arrays

可悲的是,我再次有一個小問題:-(

我現在有一個來自數據庫的多維數組,我想將其寫到表中,其中groupe值應該是列標題,而所有具有相同groupe值的記錄都應該在列的行中。

數組按組排序。

在這里我的測試數組原始數組在子數組中有8個值,但我認為它將顯示問題:

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)
) 

該表如下所示:

<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>

我認為該解決方案將是在剝離下來groupes到一個維陣列通過頭刪除重復和循環。

然后由陣列分裂groupes通過行和環...

但是我嘗試過的所有事情都無法通過對新數組進行建模而失敗...也許您知道一種更簡單的方法:)

非常感謝你 !

看來您遇到了一個常見問題,您希望將數據顯示為列而不是行。 解決方案是使用一個中間數組,在其中按組存儲數據。

因此,循環遍歷行,獲取組,如果組不存在於數組中,則創建數組。 順便說一句,您要計算組中的最大行數。 更簡單的方法是循環訪問組數組,並使用count和>。 如果組沒有該索引的數據(使用isset()),則使用FOR循環達到最大值(使用isset()),顯示並為空字符串,否則顯示名稱(或所需的數據)。

您可以使用2-3個循環來完成...:D

獎勵:您可以通過在SQL查詢中使用ORDER BY group_id來優化此算法,然后直接在第一個循環中獲得最大長度。

得到它了 :-)

我認為此解決方案可能不是最佳解決方案,但它可以工作,並且可以用於類似情況。

我做了什么:

  1. 僅從數據數組中獲取分組值並使其唯一。

  2. 將數據數組拆分為單獨的分組數組

  3. 使用行的幫助表來構建表。

這是一個示例代碼:

<?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>

謝謝 !

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM