0
for($i=0;$i<count($some_array);$i++){
   echo "<h3 name=$some_array[2][$i]>$some_array[1][$i]</h3>";      
}   

Why doesn't this work? Is it possible to echo a value of an array of an array within "" without using this annoying combining-strings-method?

echo "<h3 name=" . $some_array[2][$i] . ">" . $some_array[1][$i]. "</h3>";
1
  • 1
    Read the manual. Use "curly" syntax: php.net/manual/en/… Commented Aug 5, 2014 at 18:14

4 Answers 4

2

Try this:

echo "<h3 name={$some_array[2][$i]}>{$some_array[1][$i]}</h3>";
Sign up to request clarification or add additional context in comments.

Comments

1

It should be count($some_array[1]), because now you only count $some_array[x] and not how many keys $some_array[x][y] where y is.

Comments

1

Try

 echo '<h3 name="'.$some_array[2][$i].'">'.$some_array[1][$i].'</h3>';

Also consider the execution cost & potential error of using count within the loop. Instead consider

$cnt = count($some_array);
for($=0;$i<$count;$i++){

Comments

0

Check out the manual on Strings

Essentially, for expressions you need to use the "{}" or "Complex (curly) syntax" with double quotes.

$some_array[0]

Would work without the complex curly syntax, but you accessing another level deep of an array, hence it not working.

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.