Page 1 of 1

echoing a multi-dimensional array

Posted: Tue Jun 06, 2006 3:33 pm
by andym01480
I can't fathom out echoing an array!

Here's my code to try and understand it all

Code: Select all

print_r ($matches);
echo "<p>$matches[0][2]<p>$matches[1][2]<p>";

$first=$matches[1];
echo "0=$first[0]<p>1=$first[1]<p>2=$first[2]";
which produces

Code: Select all

Array ( [0] => Array ( [0] => Wed - Jun 07 -- Thu - Jun 08
[1] => [2] => Wed - Jun 07 -- Thu - Jun 08 [3] => [4] => Wed - Jun 07 -- Thu - Jun 08 [5] => 
) [1] => Array ( [0] => Sun - Jun 11
[1] => [2] => Sun - Jun 11 [3] => Sun - Jun 11 [4] => [5] => 
) ) 
Array[2] 

Array[2] 

0=Sun - Jun 11


1= 

2=Sun - Jun 11
why doesn't

Code: Select all

echo "$matches[0][2]";
produce

Code: Select all

Wed - Jun 07 -- Thu - Jun 08
?

Posted: Tue Jun 06, 2006 3:36 pm
by PrObLeM
try

Code: Select all

echo "<p>" . $matches[0][2] . "<p>" . $matches[1][2] . "<p>";

Posted: Tue Jun 06, 2006 3:42 pm
by andym01480
Grazie Multi.

Why can't an multidimensional array be echoed within double quotes?

Posted: Tue Jun 06, 2006 3:53 pm
by feyd
Only one level of elements is understood as it becomes a string immediately after being looked up.

Posted: Tue Jun 06, 2006 3:53 pm
by Christopher
andym01480 wrote:Why can't an multidimensional array be echoed within double quotes?
It can, you just need to help the parser out a little (same with object properties):

Code: Select all

echo "<p>{$matches[0][2]}<p>{$matches[1][2]}<p>";