1

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!

1
  • Have you tried print $val['#value'], since it looks like the key is not value, but #value. Commented Aug 2, 2009 at 15:07

3 Answers 3

3
echo $arr['#value'];

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

I see now. I thought for some reason that the "#" meant its a pointer or a reference of some sort. Thanks!
1

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 :-)

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.