Multidimensional associated array

Hi,

I try to construct an array such that each element of it is another
array returned by the fetch_array_function:

for ( $i = 1; $i<=$n; $i++ )
{
$ar[$i] = mysql_fetch_array( $result );
print "$ar[$i][fieldname]\n";
}

It prints "Array[fieldname]".

If I replace $ar[$i] by $ar everything works fine! Could you pleas
help me with that?
kurdayon [ Sa, 12 Januar 2008 04:24 ] [ ID #1906249 ]

Re: Multidimensional associated array

Kurda Yon wrote:
> Hi,
>
> I try to construct an array such that each element of it is another
> array returned by the fetch_array_function:
>
> for ( $i = 1; $i<=$n; $i++ )
> {
> $ar[$i] = mysql_fetch_array( $result );
> print "$ar[$i][fieldname]\n";
> }
>
> It prints "Array[fieldname]".
>
> If I replace $ar[$i] by $ar everything works fine! Could you pleas
> help me with that?
>

print $ar[$i]['fieldname'] . "\n";
or
print "{$ar[$i]['fieldname']\n";

[fieldname] will give you an E_NOTICE if enabled (and it should be on
your development system).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Sa, 12 Januar 2008 04:34 ] [ ID #1906250 ]

Re: Multidimensional associated array

Jerry Stuckle wrote:
> Kurda Yon wrote:
>> Hi,
>>
>> I try to construct an array such that each element of it is another
>> array returned by the fetch_array_function:
>>
>> for ( $i = 1; $i<=$n; $i++ )
>> {
>> $ar[$i] = mysql_fetch_array( $result );
>> print "$ar[$i][fieldname]\n";
>> }
>>
>> It prints "Array[fieldname]".
>>
>> If I replace $ar[$i] by $ar everything works fine! Could you pleas
>> help me with that?
>>
>
> print $ar[$i]['fieldname'] . "\n";
> or
> print "{$ar[$i]['fieldname']}\n";
>
> [fieldname] will give you an E_NOTICE if enabled (and it should be on
> your development system).
>

A notice is not thrown in this case because the key is already within
double quotes. The problem is fixed by the necessity to use { }'s when
using multi-dimensional arrays within double quotes. I normally use this
method. To get PHP to throw a NOTICE, remove the single quotes from your
second example.

--
Norman
Registered Linux user #461062
Norman Peelman [ Sa, 12 Januar 2008 06:14 ] [ ID #1906252 ]

Re: Multidimensional associated array

print_r will show you the internals
Peter Pei [ So, 13 Januar 2008 20:14 ] [ ID #1906733 ]

Re: Multidimensional associated array

no point for you to control the index yourself
<?php
$a[] = 2;
$a[] = 3;
$a[] = 5;
print $a;
print_r($a);
?>
Peter Pei [ So, 13 Januar 2008 20:18 ] [ ID #1906734 ]

Re: Multidimensional associated array

<?php
$a = array(array("a"=>1, "b"=>2));
print "{$a[0]['b']}";
?>
Peter Pei [ So, 13 Januar 2008 20:25 ] [ ID #1906735 ]

Re: Multidimensional associated array

you obviously forgot to close your }
Peter Pei [ So, 13 Januar 2008 20:27 ] [ ID #1906736 ]

Re: Multidimensional associated array

<?php
$a = array(array("a"=>1, "b"=>2));
print "{$a[0][b]}";
?>
this gives you notice,
Peter Pei [ So, 13 Januar 2008 20:29 ] [ ID #1906737 ]

Re: Multidimensional associated array

php ends the variable name at the most convenience spot
Peter Pei [ So, 13 Januar 2008 20:30 ] [ ID #1906739 ]

Re: Multidimensional associated array

Peter Pei wrote:
> <?php
> $a = array(array("a"=>1, "b"=>2));
> print "{$a[0][b]}";
> ?>
> this gives you notice,

Yes, that's what I said. When using multi-dimensional arrays within
double-quoted strings you must use the { }'s... and when you use the {
}'s you must use single quotes around the key names. PHP does not search
for CONSTANTS within double-quoted strings, there is a very specific
reason for it.


--
Norman
Registered Linux user #461062
Norman Peelman [ Mo, 14 Januar 2008 01:20 ] [ ID #1907454 ]
PHP » comp.lang.php » Multidimensional associated array

Vorheriges Thema: PHP mail marked is phishing
Nächstes Thema: Can a form change session variables?