timT
timT

Reputation: 287

Trouble getting data from an array in PHP

I've got this array:

array(1) { 
   ["sensory-evaluation"]=> array(6) { 
     ["name"]=> string(18) "Sensory    Evaluation" 
     ["value"]=> string(43) "10/22/2015 at 6:00pm | 11/25/2015 at 6:00pm"
     ["position"]=> string(1) "3" 
     ["is_visible"]=> int(1) 
     ["is_variation"]=> int(1) 
     ["is_taxonomy"]=> int(0) 
    } 
} 

I need to be able to get the data from ["value"]. If I do $arr["sensory-evaluation"]["value"] I can get it but the problem is ["sensory-evaluation"] will be different for each element in my array so I need a way to abstract that part but I haven't been able to figure it out.

Upvotes: 0

Views: 38

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 78984

If you have only one item in the array as you show then:

echo current($arr)['value'];

If you don't know if it exists:

if(isset(current($arr)['value'])) {
    echo current($arr)['value'];
}

You could also do:

echo array_values($arr)[0]['value'];

Upvotes: 1

jstuartmilne
jstuartmilne

Reputation: 4488

why not use a for each

function getValue($arr){
    foreach ($arr as $key => $value) {
        var $subArray=$arr[$key];
       if( array_key_exists ('value' ,$subArray))
         return $arr[$key]['value'];
    }
}

Upvotes: 0

Related Questions