2013-02-19 51 views
-3
Array ( 

    [x0] => sometext1  
    [x1] => sometext2  
    [x2] => sometext3  
    [x3] => sometext4  
    [x4] => sometext5  
    [x5] => sometext6 
    [x?] => sometext? 

    [y0] => someothertext1  
    [y1] => someothertext2 
    [y2] => someothertext3  
    [y3] => someothertext4  
    [y4] => someothertext5  
    [y5] => someothertext6 
    [y?] => someothertext? 

    ) 

我試圖採取這個數組並創建一個表。基本上我不知道會有多少x或y ...我目前找不到任何解決方案。任何幫助將不勝感激。PHP陣列到表沒有運氣

的結果,我試圖讓

x  |  y 
sometext1 | someothertext1 
sometext2 | someothertext2 
...  | ... 
+2

你想要什麼表格? – 2013-02-19 07:04:57

+2

這些x和y行和colom是什麼? – 2013-02-19 07:05:02

+0

x和y將是列。 我不知道如何獲得這些值的錶行。 – user1605661 2013-02-19 07:06:26

回答

0

複製陣列到另一個變種在這個例子中,你有兩個數組與名稱$ ARR和$ ARR2這只是一個副本每。

$arr = $arr2 = Array ( ....); 

foreach($arr as $key=>$data) 
{ 
    $num = substr($key,1); 
    echo '<tr><td>'.$data.'</td>'; 
    foreach($arr2 as $key2=>$data2) 
    { 
     $num2 = substr($key2,1); 
     if($num == $num2) 
     { 
      echo '<td>'.$data.'</td>'; 
      break; 
     } 
    } 
    echo '</tr>'; 
} 

當然這並沒有考慮到的事情一樣,如果沒有「Y」相匹配的「X」額外的固定缺少TD。

1

如果可能的話,對數組做一個小的修改,以及如何聲明它。使它看起來是這樣的:

Array ( 
    [x] => Array (
     [0] => sometext1  
     [1] => sometext2  
     [2] => sometext3  
     [3] => sometext4  
     [4] => sometext5  
     [5] => sometext6 
     [?] => sometext? 
    ) 
    [y] => Array (
     [0] => sometext1  
     [1] => sometext2  
     [2] => sometext3  
     [3] => sometext4  
     [4] => sometext5  
     [5] => sometext6 
     [?] => sometext? 
    ) 
) 

並以這種方式,使用PHP,你可以做的表是這樣的:

<table> 
<?php 
    echo '<tr>'; 
    foreach ($array as $heading => $contents) 
     echo "<th>$heading</th>"; 
    echo '</tr>'; 
    foreach ($array as $heading => $contents) 
     foreach ($heading as $value) 
      echo "<td>$value</td>"; 
?> 
</table> 
+0

謝謝@Praveen,但我其實我不知道如何將它轉換爲您的版本。你能幫忙弄清楚嗎? – user1605661 2013-02-19 07:15:40

+0

@ user1605661:查看我對此格式的答案 – 2013-02-19 07:17:11

+0

@ user1605661好的,那麼Prasanth的回答將適合您。礦井對於可維護性會更好。 – 2013-02-19 07:18:07

0

由於普利文庫馬爾解釋表看起來像這樣:

# X    y 
0 sometext1  sometext1 
1 sometext2  sometext2 
1

試試這個:

<?php 
$array = Array('x0' => 'sometext1',  
       'x1' => 'sometext2',  
       'x2' => 'sometext3',  
       'x3' => 'sometext4',  
       'x4' => 'sometext5',  
       'x5' => 'sometext', 
       'y0' => 'someothertext1',  
       'y1' => 'someothertext2', 
       'y2' => 'someothertext3',  
       'y3' => 'someothertext4',  
       'y4' => 'someothertext5',  
       'y5' => 'someothertext6', 
       ); 

$res = array();    
foreach($array as $key=>$val){ 
    preg_match('/(?P<var>\w{1})(?P<ky>\d+)/',$key, $match); 
    $res[$match['ky']][$match['var']] = $val; 
} 


echo "<table>"; 
echo "<tr><td>X</td><td>Y</td></tr>"; 
foreach($res as $keys=>$vals){ 
    echo "<tr><td>".$vals['x']."</td><td>".$vals['y']."</td></tr>"; 
} 
echo "<table>"; 

?> 

輸出:

X   Y 
sometext1 someothertext1 
sometext2 someothertext2 
sometext3 someothertext3 
sometext4 someothertext4 
sometext5 someothertext5 
sometext someothertext6 
+0

嘗試使用此數組''array = Array('x0'=>'sometext1', 'x1'=>'sometext2', 'x2'=>'sometext3', 'x3'=>'sometext4', 'X4'=> 'sometext5', 'X11'=> 'sometext', 'Y0'=> 'someothertext1', 'Y1'=> 'someothertext2', 'Y2'=> 'someothertext3', 'Y3'=> 'someothertext4', 'Y4'=> 'someothertext5', 'Y11'=> 'someothertext6', );' – 2013-02-19 07:24:14

+0

編輯答案,我想這是你需要 – 2013-02-19 07:26:06

+0

一個@SherinJose :它可以很好地與您的數組 – 2013-02-19 07:28:47