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

这是我尝试过的代码,但是每次 for 循环迭代时 foreach 循环都会迭代多次。

4

1 回答 1

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

注意:只要数组的长度短于索引,就会收到很多关于数组不存在索引的警告,但这应该是有效的并且执行得很好。另外,我没有测试它,所以很容易出现拼写错误。

于 2013-02-25T20:25:13.993 回答