2013-02-25 101 views
0

我有5個陣列顯示多個陣列在一個表

$array1 = array('ar1_1','ar1_2','ar1_3','ar1_4'); 
    $array2 = array('ar2_1','ar2_2','ar2_3','ar2_4','ar2_5'); 
    $array3 = array('ar3_1','ar3_2','ar3_3','ar3_4','ar3_5','ar3_6'); 
    $array4 = array('ar4_1','ar4_2','ar4_3','ar4_4'); 
    $array5 = array('ar5_1','ar5_2','ar5_3'); 
    $loc = array('AM','IN','US'); 

我想以這樣的方式將其顯示;

<table> 
<tr class="d1 vevent"> 
<td> 
<b><abbr class="dtstart" title="' . $array1 . '">' . $array2 . '</abbr></b> <br>' . $array3 . '</td> 
<td> 
<span class="summary">' . $array4 . ' - ' . $array5 . '</span> 
<span class="location">' . $loc . '</span> 
</td> 
</tr> 
</table> 

任何解決方案?

我試過for循環,foreach循環,zip,array_merge和array_combine,但無法達到預期的結果。

我已經試過代碼:

echo '<table width="100%" border="1" cellspacing="0" cellpadding="5"> 
<tbody> 
<tr class="toprow"> 
<td><strong>Date </strong></td> 
<td><strong> Details</strong></td> 
</tr>'; 


/* 
foreach(array_combine($date, $time) as $time1 => $date1) 
{ 
@$date1 = split('[()]', $date1); 
    echo $time1. $date1[1]. "<br />"; 
} */ 




$result = count($match_no); 
for($i=0; $i<$result; $i++) { 
echo ' 
<tr class="d1 vevent"> 
<td> 
<b><abbr class="dtstart" title="'; 
echo trim($date[0]); 
echo '">'; 
echo $date[0]; 
echo '</abbr></b> <br>'; 

foreach($time as $time1) 
{ 
@$time1 = split('[()]', $time1); 
    echo $time1[1]; 
} 
echo '</td> 
<td> 
<span class="summary">'; 
echo $match_no[0]; 
echo ' - <a href="'; 
echo '#" class="url">'; 
echo $team[0]; 
echo ' vs '; 
echo $team[1]; 
echo '</a></span>'; 
echo '<span class="location">'; 
echo $location; 
echo '</span>'; 
echo '</td> 
</tr>'; 

} 

echo '</table>'; 

這是代碼我試過,但foreach循環迭代多的時間循環迭代每次。

+0

您對桌子外觀的描述很模糊。你什麼意思? – ajon 2013-02-25 20:10:52

+0

向我們展示您嘗試過的循環? /你如何獲得數組?靜態或數據庫 – UnholyRanger 2013-02-25 20:10:55

+0

@unholyRanger代碼添加 – Nandla 2013-02-25 20:16:18

回答

0

我可以推薦你使用這個類嗎?像下面這樣:

class myClass(){ 
    public $title; 
    public $var1; 
    public $var2; 
    public $var3; 
    public $var4; 
    public $var5; 
    public $loc; 

    public function __construct($title, $var1, $var2, $var3, $var4, $var5){ 
     $this->title = $title; 
     $this->var1 = $var1; 
     $this->var2 = $var2; 
     $this->var3 = $var3; 
     $this->var4 = $var4; 
     $this->var5 = $var5; 
    } 

    public function printTable(){ 
    echo ' 
     <tr class="d1 vevent"> 
      <td> 
      <b><abbr class="dtstart" title="' . $this->var1 . '">' . $this->var2 . '</abbr></b> <br>' . $this->var3 . '</td> 
      <td> 
       <span class="summary">' . $this->var4 . ' - ' . $this->var5 . '</span> 
       <span class="location">' . $this->loc . '</span> 
      </td> 
     </tr>'; 
    } 
} 

然後你就可以創建使用您的陣列中的數據,如對象:

$obj = myClass($ar1_1,$ar1_2,$ar1_3, $ar1_5,$loc); 
//Then print the table: 
$obj->printTable(); 

或者,如果你有對象的數組:

echo "<table>"; 
for($objArray as $obj){ 
    $obj->printTable(); 
} 
echo "</table>"; 

最後,如果你想使用你現有的陣列系統使用這個類,你可以做以下事情:

$longestArray = $array1; 
if(count($array2)>count($longestArray)) $longestArray = $array2; 
if(count($array3)>count($longestArray)) $longestArray = $array3; 
if(count($array4)>count($longestArray)) $longestArray = $array4; 
if(count($array5)>count($longestArray)) $longestArray = $array5; 
if(count($loc)>count($longestArray)) $longestArray = $loc; 

$objArray = Array(); 
for($i=0;$i<count($longestArray);$i++){ 
    $objArray[] = myClass($array1[$i],$array2[$i],$array3[$i], $array4[$i], $array5[$i], $loc[$i]); 
} 

echo "<table>"; 
for($objArray as $obj){ 
    $obj->printTable(); 
} 
echo "</table>"; 

注意:只要數組的長度比索引短,就會得到很多關於索引不存在的索引的警告,但這應該是有效的並且執行得很好。另外,我沒有測試它,所以很容易出現拼寫錯誤。

+0

你可以更詳細地闡述它 – Nandla 2013-02-25 20:27:12

+0

其實我很窮陣列:( 謝謝,我會給它在這裏試試並更新結果 – Nandla 2013-02-25 20:32:28

+0

你可以看到我的更新使用你當前的代碼 – ajon 2013-02-25 20:40:46