0

I'm trying to access a piece of data in an array of arrays that (I believe) is in an object (this may not be the right term though).

When I do print_r on this: $order_total_modules->process() I get...

Array ( [0] => Array ( [code] => ot_subtotal [title] => Sub-Total: [text] => $49.99 [value] => 49.99 [sort_order] => 1 ) [1] => Array ( [code] => ot_total [title] => Total: [text] => $0.00 [value] => 0 [sort_order] => 12 ) ) 

If I run echo $order_total_modules->process()[1][3];, I should get "0", because that is the 3rd element of the 2nd array... right? Yet, I get an error.

Can anyone help with this?

3
  • It's the third but not 3, it is value. Commented Dec 10, 2014 at 21:58
  • Array dereferencing is only available as of >=PHP5.4 Commented Dec 10, 2014 at 21:58
  • That's an associative array, not a numeric-indexed one. [1]['value'] Commented Dec 10, 2014 at 21:59

3 Answers 3

1

Even though it is the third element counting from 0, the index is not 3 it is an associative array with the index value:

Available in PHP >=5.4.0:

echo $order_total_modules->process()[1]['value']; 

Or PHP < 5.4.0:

$result = $order_total_modules->process(); echo $result[1]['value']; 
Sign up to request clarification or add additional context in comments.

2 Comments

Hope you don't mind the edit - it's 5.4+, not 5.5 for array dereferencing.
Michael beat me to it :)
0

You cannot access an associative array via an integer index(unless the index is an actial integer). So in this case use : [1]['code'] to access what woulde be [1][0] with a 'normal' array.

Comments

0

Try putting it in a var first:

$ar = $order_total_modules->process(); echo $ar[1]['value']; 

The second level array is an assoc, which means that the key is not numeric, which means that you need to call the name of the key, hence the 'value'.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.