Multidimensional associated array
Multidimensional associated array
am 12.01.2008 04:24:12 von kurdayon
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?
Re: Multidimensional associated array
am 12.01.2008 04:34:20 von Jerry Stuckle
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@attglobal.net
==================
Re: Multidimensional associated array
am 12.01.2008 06:14:20 von Norman Peelman
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
Re: Multidimensional associated array
am 13.01.2008 20:14:40 von Peter Pei
print_r will show you the internals
Re: Multidimensional associated array
am 13.01.2008 20:18:07 von Peter Pei
no point for you to control the index yourself
$a[] = 2;
$a[] = 3;
$a[] = 5;
print $a;
print_r($a);
?>
Re: Multidimensional associated array
am 13.01.2008 20:25:58 von Peter Pei
$a = array(array("a"=>1, "b"=>2));
print "{$a[0]['b']}";
?>
Re: Multidimensional associated array
am 13.01.2008 20:27:28 von Peter Pei
you obviously forgot to close your }
Re: Multidimensional associated array
am 13.01.2008 20:29:16 von Peter Pei
$a = array(array("a"=>1, "b"=>2));
print "{$a[0][b]}";
?>
this gives you notice,
Re: Multidimensional associated array
am 13.01.2008 20:30:16 von Peter Pei
php ends the variable name at the most convenience spot
Re: Multidimensional associated array
am 14.01.2008 01:20:33 von Norman Peelman
Peter Pei wrote:
>
> $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