views:

91

answers:

3

I have the following array (in php after executing print_r on the array object):

Array ( 
   [#weight] => 0 
   [#value] => Some value.
)

Assuming the array object is $arr, how do I print out "value". The following does NOT work:

print  $arr->value;
print  $val ['value'] ;
print  $val [value] ;

So... how do you do it? Any insight into WHY would be greatly appreciated! Thanks!

+3  A: 
echo $arr['#value'];

The print_r() appears to be telling you that the array key is the string #value.

chaos
+1  A: 

After quickly checking the docs, it looks like my comment was correct.

Try this code:

print $arr['#value'];

The reason is that the key to the array is not value, but #value.

Thomas Owens
I see now. I thought for some reason that the "#" meant its a pointer or a reference of some sort. Thanks!
RD
+1  A: 

Hi,

You said your array contains this :

Array ( 
   [#weight] => 0 
   [#value] => Some value.
)

So, what about using the keys given in print_r's output, like this :

echo $arr['#value'];

What print_r gives is the couples of keys/values your array contains ; and to access a value in an array, you use $your_array['the_key']


You might want to take a look at the PHP manual ; here's the page about arrays.
Going through the chapters about the basics of PHP might help you in the future :-)

Pascal MARTIN