Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组未返回整个元素_Php_Arrays - Fatal编程技术网

Php 数组未返回整个元素

Php 数组未返回整个元素,php,arrays,Php,Arrays,我想我只是有一些无效的语法,但就我个人而言,我无法理解它。我有一个包含3个元素的嵌套数组 $screenshot = array( array( Carousel => "Library.png", Caption => "Long caption for the library goes here", ListItem => "The Library")); 我使用for循环来编写一些包含元素的HTML <?php for ( $row = 0; $r

我想我只是有一些无效的语法,但就我个人而言,我无法理解它。我有一个包含3个元素的嵌套数组

$screenshot = array( array( Carousel => "Library.png", Caption => "Long caption for the library goes here", ListItem => "The Library"));
我使用for循环来编写一些包含元素的HTML

<?php
        for ( $row = 0; $row < 4; $row++)
        {
            echo "<div class=\"feature-title\"><a href=" . $carousel_dir . $screenshot[$row]["Carousel"] . " title=" . $screenshot[$row]["Caption"] . " rel=\"lightbox[list]\">" . $screenshot[$row]["ListItem"] . "</a></div>";
        }           
        ?>

我的问题是“a”标记的“title”部分只包括第一个单词Long。因此,在上述情况下:

<div class="feature-title"><a href="/images_carousel/1A-Library.png" title="Long" caption for the library goes here rel="lightbox[list]" class="cboxElement">The Library</a></div>


谁能解释一下我的错误吗?提前感谢。

您忘记了属性值周围的双引号,因此只有第一个单词才算。其余的变为(无效)属性名。

养成习惯,将数组索引用双引号括起来,即字符串索引的“

$screenshot = array( 
    array( 
        "Carousel" => "Library.png", 
        "Caption" => "Long caption for the library goes here", 
        "ListItem" => "The Library")
    );
请使用此代码

echo "<div class=\"feature-title\"><a href='" . $carousel_dir . $screenshot[$row]["Carousel"] . "' title='" . $screenshot[$row]["Caption"] . "' rel=\"lightbox[list]\">" . $screenshot[$row]["ListItem"] . "</a></div>";
echo”“;

正如Starx和Ignacio所提到的,数组的关键部分需要被引用。但是,它们是单引号还是双引号并不重要。如果启用更详细的日志记录(如E_ALL而不是E_ALL&~E_NOTICE),您将收到如下消息:

PHP注意:在第3行的badarray.PHP中使用未定义的常量Carousel-假定为“Carousel”

PHP注意:在第3行的badarray.PHP中使用未定义的常量标题-假定为“Caption”

PHP注意:在第3行的badarray.PHP中使用未定义的常量ListItem-假定为'ListItem'

PHP尝试查找已使用这些名称定义的常量。如果找不到,它会假定您指的是该值的字符串。这意味着,如果您确实定义了名为“Carousel”、“Caption”或“ListItem”的常量,它将更改您定义的数组中的键值

我看到的另一个问题是,可能只包含了部分代码,因为外部数组中只有一个数组,所以当您访问$screenshot[$row]时,一旦循环将$row增量增加到0以上,就不会有任何内容


如果您可以提供使用该数组试图输出的内容,我可以帮助您构建该代码。

谢谢,我知道这必须很简单。我已经阅读了有关两种方法的内容,但我将坚持这一路线,谢谢。您忘记了
href
的引号。