0

I have an a value in an array that I am not sure how to reference.

If I show the array like this:

 while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
 print_r( $this->contents[$products_id]['attributes']); echo "<br/>"; 
}

I get the following output:

Array ( [5] => 115 [2] => 17 ) 
Array ( [5] => 115 [2] => 17 ) 

The value that I need is the 17.

asked May 27, 2013 at 15:17
4
  • Why the old PHP4 way to get through an array (with list + each) ? Even if for this problem, you should not even think about doing this kind of action Commented May 27, 2013 at 15:51
  • Can you please explain? This is not my code fully. What is the new way? Commented May 27, 2013 at 16:54
  • I should say, what is the php5 way of going through an array and why is this way so bad? Commented May 27, 2013 at 17:00
  • You have to use the foreach keyword : foreach ($container as $key => $value) { /* actions */ } (one line here, because on a SO comment, you can't paste multi line code ;). It is a loop, when on each turn, $key will be the key of the current element (equivalent of the first argument of list), and $value its value (2nd argument). Commented May 27, 2013 at 19:53

1 Answer 1

2
$this->contents[$products_id]['attributes'][2]
answered May 27, 2013 at 15:19

1 Comment

This works so thank you! And now looking at it seams obvious. I thought the 115 and the [2] were connected. doh

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.