Bame
Bame

Reputation: 3

How to get one part of a multidimensional array using php?

I have a problem with a multidimensional array, I want to save specific parts of an array to later show the information on the page but I just cant get it to work

this is the Array when I var_dump it:

array(1) { 
    ["500040477"]=> array(1) { 
        ["statistics"]=> array(1) { 
            ["all"]=> array(1) { 
                ["frags"]=> int(23816) 
            } 
        } 
    } 
} 

now I want to get the frags and be able to save the int in a extra array/variable

I tried a lot and nothing works even the "common" method to access it doesn't work :(

Upvotes: 0

Views: 399

Answers (2)

Jack jdeoel
Jack jdeoel

Reputation: 4584

If you want to save specific parts of an array ,you can write your own function for this

//first param arra ,second param key
function findByKey($array,$k) { 
    if(isset($array[$k])) {     
        return $array[$k];
    }
    else {
        if(is_array($array)) return findByKey(current($array),$k);
        else return "Key don't exist";
    }
}

You can use above function to get specific array value using key .As your question

findByKey($yourarray,"frags");

Upvotes: 0

user2342558
user2342558

Reputation: 6712

In the case showed in your example:

$frags = $nameOfYourArray["500040477"]["statistics"]["all"]["frags"];

For arrays with the first key with different name (instead of 500040477):

$arrayFirstkey = current($array);
$frags = $arrayFirstkey["statistics"]["all"]["frags"];

See current PHP function.

Upvotes: 1

Related Questions