1

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.

4 Answers 4

1

You forgot double quotes around the attribute values, so only the first word counts. The rest become (invalid) attribute names.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I knew it had to be something simple.
1

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

1 Comment

I've read about doing it both ways but I will stick to this route, thanks.
1

As Starx and Ignacio have mentioned, the key parts of the arrays need to be quoted. It doesn't matter if they are single or double quotes though. If you turn on more verbose logging (like E_ALL instead of E_ALL & ~E_NOTICE) you'll get messages like this:

PHP Notice: Use of undefined constant Carousel - assumed 'Carousel' in badarray.php on line 3

PHP Notice: Use of undefined constant Caption - assumed 'Caption' in badarray.php on line 3

PHP Notice: Use of undefined constant ListItem - assumed 'ListItem' in badarray.php on line 3

PHP tries to find a constant that has been defined with those names. When it cannot find one it assumes you meant the string of that value. This means that if you ever did define a constant named "Carousel", "Caption" or "ListItem" it would change the key values in the array you defined.

The other issue I see, and it could just be that only partial code was included, was there is only a single array inside of the outer array so when you're accessing $screenshot[$row], there will be nothing there once your loop increments $row beyond 0.

If you can provide what you're trying to output from using that array, I can help you build that code.

Comments

0

please use this code

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

1 Comment

You forgot the quotes for href.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.