2

I'm trying to figure out why it is that I cannot access the follow array with this statement:

var_dump($thevar[0]['product_id']);

Array
(
    [d142d425a5487967a914b6579428d64b] => Array
        (
            [product_id] => 253
            [variation_id] => 
            [variation] => 
            [quantity] => 1
            [data] => WC_Product Object
                (
                    [id] => 253
                    [product_custom_fields] => Array
                        (
                            [_edit_last] => Array
                                (
                                    [0] => 1
                                )

                            [_edit_lock] => Array
                                (
                                    [0] => 1345655854:1
                                )

                            [_thumbnail_id] => Array
                                (
                                    [0] => 102
                                )

I can, however, access the 'product_id' using the dynamically created array name:

print_r($thevar['d142d425a5487967a914b6579428d64b']['product_id']);

The issue is, I don't know what that dynamic name is going to be on the fly...

3 Answers 3

2

There are several options for such scenarios.

Manually iterate over the array

You can use reset, next, key and/or each to iterate over the array (perhaps partially).

For example, to grab the first item regardless of key:

$item = reset($thevar);

Reindex the array

Sometimes it's just convenient to be able to index into the array numerically, and a small performance hit is not a problem. In that case you can reindex using array_values:

$values = array_values($thevar);
$item = $values[0]; // because $values is numerically indexed

Iterate with foreach

This would work for a single value as well as it works for more, but it might give the wrong impression to readers of the code.

foreach($thevar as $item) {
    // do something with $item
}
Sign up to request clarification or add additional context in comments.

Comments

1

If the array key is dynamic you might find the PHP function array_keys() useful.

It will return an array of the keys used in an array. You can then use this to access a particular element in the array.

See here for more:

http://php.net/manual/en/function.array-keys.php

Comments

0

Because PHP array are associative therefor you have to access them by key.

But you may use reset($thevar) to get first item.

Or array_values():

array_values($thevar)[0]

Or if you feel like overkill you may also use array_keys() and use the [0] element to address element like this:

$thevar[ array_keys($thevar)[0]]

2 Comments

So I in fact DO need to find out what that Key is in order to navigate it.
No, you may also use array_values (as I mentioned), but that would create new array. Using reset() should be really the fastest way to do this.

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.