Max Frai
Max Frai

Reputation: 64276

Accessing elements in array

I have a variable in php, print_r:

Array ( 
    [0] => Array ( 
        [0] => Array ( 
            [sum(`Lgsl`.`players`)] => 7769 
        ) 
    )
)

And I have a few such arrays with similiar structure. What is the easiest way to get 7769 number from it without referencing to string-key in latest array.

Upvotes: 0

Views: 140

Answers (2)

devdRew
devdRew

Reputation: 4571

I think that array_search()...

For example you have array $a, which is one of yours. Then you can access somehow like this:

 $var = $a[0][0][array_search(7769, $a[0][0])]; // to do it perfect, you can add isset() check.

Hope, that's is exactly what you need.

UPDATE:

Tested by:

$a = array(0 => array(0 => array('some' => 7769)));

$var = $a[0][0][array_search(7769, $a[0][0])];

echo $var;

Got 7769.

Upvotes: 0

Yogu
Yogu

Reputation: 9445

list ($number) = array_values($arr[0][0]);

The function array_values returns a list of all values with a numeric index, so that you can access the first and only element via index 0. The list keyword can extract array elements into separate variables.

Upvotes: 2

Related Questions