gtilflm
gtilflm

Reputation: 1465

Accessing Array Data in Object

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?

Upvotes: 0

Views: 42

Answers (3)

AbraCadaver
AbraCadaver

Reputation: 78994

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'];

Upvotes: 1

Thanasis Pap
Thanasis Pap

Reputation: 2051

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'.

Upvotes: 0

g_uint
g_uint

Reputation: 2031

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.

Upvotes: 0

Related Questions