WASEEM A WASEEM A - 4 months ago 6
PHP Question

How to access a multidimensional array?

I am new to php. I am able to fetch the records from database and storing them into an array. Array contains more than one values for each array index. Now I want to fetch only one value. Here is my array:

Array ( [0] => Array ( [center_id] => 103 [center_strength] => 800 )
[1] => Array ( [center_id] => 102 [center_strength] => 400 )
[2] => Array ( [center_id] => 101 [center_strength] => 200 )
)


From this array I want to get the value of
[center_strength]
only i.e. only
800
. Please Help me :)

Answer

Suppose your array variable name is the $array. So you can access center_strength just like $array[0]['center_strength'] and if you want iterate over all elements:

foreach($array as $single_arr) {
   //Now $single_arr contain the inner array
   foreach($single_arr as $key => $value) {
     //for first item: $key= center_id , $value=103
   }
}