March 1, 2012
Array Not Returning Entire Element
Question by Bill
I think I simply have invalid syntax, but for the life of me I can’t figure it out. I have a nested array with 3 elements.
$screenshot = array( array( Carousel => "Library.png", Caption => "Long caption for the library goes here", ListItem => "The Library"));
I’m using a for loop to compose some HTML that includes the elements.
<?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>";
}
?>
My problem is that the “title” portion of the “a” tag only includes the first word, Long. So in the above case it would be:
<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>
Can anyone shed some light on my error? Thanks in advance.
Answer by Ignacio Vazquez-Abrams
You forgot double quotes around the attribute values, so only the first word counts. The rest become (invalid) attribute names.
Answer by Starx
Make a habit of wrapping, the array index within double-quotes i.e. "
for string indexes.
$screenshot = array(
array(
"Carousel" => "Library.png",
"Caption" => "Long caption for the library goes here",
"ListItem" => "The Library")
);