[英]How to access a multidimensional array?
I am new to php.我是 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 ie only 800
.从这个数组中,我只想获得[center_strength]
的值,即只有800
。 Please Help me :)请帮我 :)
Suppose your array variable name is the $array.假设您的数组变量名称是 $array。 So you can access center_strength
just like $array[0]['center_strength']
and if you want iterate over all elements:所以你可以像$array[0]['center_strength']
一样访问center_strength
,如果你想遍历所有元素:
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
}
}
// Printing all the keys and values one by one
$keys = array_keys($array);
for($i = 0; $i < count($array); $i++) {
echo $keys[$i] . "{<br>";
foreach($array[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "<br>";
}
echo "}<br>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.