无法访问array()PHP的元素


Unable to access an element of an array() PHP

我有14个li元素,其中包含各种信息,我认为最好将其全部封装到php循环中,并使用变量和数组来填补空白。

我遇到了两个问题。首先,我不会从数组描述或标题中返回任何元素。

下一个问题是文件名$iFINAL.pdf——它应该只是附加了FINAL的变量$i

我通常不会使用EOT,但在这种情况下,它似乎比转义所有各种引号快得多。

感谢您的帮助!

    <?php
$description = array("Decription 1 here","description 2 here");
$title = array("title 1","titlesfdfs ","sdfsdsd","wqeqe","","");
for($i=1; $i <= 14; $i++){
    if($i < 10){
        $i = "0".$i;
    }
$body = <<<EOT
<h3><a href="#">Chapter $i - $title[$i]</a></h3>
<div class=trainingItemListContainer>
    <div class="mainDetails">
        <p><strong>Introduction:</strong> $description[$i]</p>
    </div>
    <div class="subDetails">
        <div class="viewAndDownload">
            <a href="training_chap$i.php"><p>Click to view the chapter</p></a>
        </div>
        <div class="viewAndDownload">
            <a href="../download.php?filename=/trainingHandoutPDF/$iFINAL.pdf">&nbsp;&nbsp;&nbsp;&nbsp;Click to download the PDF file&nbsp;&nbsp;&nbsp;<img src="../images/disk.png" alt="downloadIcon" border="0"/></a>
        </div>
    </div>
</div>
EOT;
echo $body;
}

您的代码有两个主要问题:

  • 您正在将$i的值设置为0102等。这将导致脚本产生"未定义索引"错误,因为您的数组中没有此类索引
  • 您没有将变量封装在{ }中。如果将其括起来,将使用实际的变量值。例如:{$i}

示例:

$body = <<<EOT
<h3><a href="#">Chapter {$i} - {$title[$i]}</a></h3>
<div class=trainingItemListContainer>
    <div class="mainDetails">
        <p><strong>Introduction:</strong> {$description[$i]}</p>
    </div>
    <div class="subDetails">
        <div class="viewAndDownload">
            <a href="training_chap$i.php"><p>Click to view the chapter</p></a>
        </div>
        <div class="viewAndDownload">
            <a href="../download.php?filename=/trainingHandoutPDF/{$i}FINAL.pdf">&nbsp;&nbsp;&nbsp;&nbsp;Click to download the PDF file&nbsp;&nbsp;&nbsp;<img src="../images/disk.png" alt="downloadIcon" border="0"/></a>
        </div>
    </div>
</div>
EOT;

这里的几个问题:

(1)此代码导致问题:

if($i < 10){
    $i = "0".$i;
}

为什么如果你做了一个var_dump($i)(=简单的调试),你就会意识到

$title[1]不同于$title['01']

解决方案:删除上述代码。

(2)PHP中的数组将以索引0而不是1开头。

echo $description[1];

将在此处输出"描述2"。

解决方案

for ($i = 0; $i < 14; $i++) {
   $number = str_pad(($i + 1), 2, "00", STR_PAD_LEFT);
   ...
   <h3><a href="#">Chapter $number - $title[$i]</a></h3>

看到它在这里工作:http://codepad.viper-7.com/489laJ

您的代码中有一个问题,您正在更改$i的值(在此处重新分配另一个值):

if($i < 10){
    $i = "0".$i;
}

您也可以使用另一个变量,如$j

 if($i < 10){
    $j = "0".$i;
 }else{
    $j = $i;
 }

这可能不是您的确切问题,但它可能会帮助您改进代码。

为什么不试试这个?

<?php
$description = array("Decription 1 here","description 2 here");
$title = array("title 1","titlesfdfs ","sdfsdsd","wqeqe","","");
for($i=1; $i <= 14; $i++){
    if($i < 10){
        $i = "0".$i;
    }
?>
<h3><a href="#">Chapter <?php print($i." - ".$title[$i]); ?></a></h3>
<div class=trainingItemListContainer>
    <div class="mainDetails">
        <p><strong>Introduction:</strong> <?php print($description[$i]); ?></p>
    </div>
    <div class="subDetails">
        <div class="viewAndDownload">
            <a href="training_chap<?php print($i); ?>.php"><p>Click to view the chapter</p></a>
        </div>
        <div class="viewAndDownload">
            <a href="../download.php?filename=/trainingHandoutPDF/<?php print($i);?>FINAL.pdf">&nbsp;&nbsp;&nbsp;&nbsp;Click to download the PDF file&nbsp;&nbsp;&nbsp;<img src="../images/disk.png" alt="downloadIcon" border="0"/></a>
        </div>
    </div>
</div>

如果您稍后有PHP代码,只需再次打开<?标记即可!