我有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>
任何解决方案?
我尝试过循环,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 :(得分: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>";
注意:只要数组的长度小于索引,就会收到很多关于数组不存在的索引的警告,但是这应该是有效的并且执行得很好。另外,我没有测试它,所以很容易出现错别字。