0

The code below is only printing the first letter of each array item. It's got me quite baffled.

require_once 'includes/global.inc.php';
print_r($site->bookmarkTags(1));

$index = 0;
foreach ($site->bookmarkTags(1) as $tag) {
    echo $tag['$index'];
    $index = $index + 1;
}

print_r return:

Array ( [0] => Wallpapers [1] => Free )

the loop:

WF
0

3 Answers 3

3

Try echo $tag, not $tag['$index']

Since you are using foreach, the value is already taken from the array, and when you post $tag['$index'] it will print the character from the '$index' position :)

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

1 Comment

Why does PHP even do that... or rather did. I'm aware they fixed it in 5.4+.
1

It seems you've attempted to do what foreach is already doing...
The problem is that you're actually echoing the $index letter of a non-array because foreach is already doing what you seem to be expecting your $index = $index+1 to do:

require_once 'includes/global.inc.php';
print_r($site->bookmarkTags(1));

$index = 0;
foreach ($site->bookmarkTags(1) as $tag) {
    echo $tag; // REMOVE [$index] from $tag, because $tag isn't an array
    $index = $index + 1; // You can remove this line, because it serves no purpose
}

Comments

0
require_once 'includes/global.inc.php';

// Store the value temporarily instead of
// making a function call each time
$tags = $site->bookmarkTags(1);
foreach ($tags as $tag) {
    echo $tag;
}

This should work. The issue might be because you're making a function call every iteration, versus storing the value temporarily and looping over it.

Comments

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.