我有一个数组,我使用print_r
,这就发生了:
Array
(
[141] => 1
[171] => 3
[156] => 2
[241] => 1
[271] => 1
[256] => 1
[341] => 1
[371] => 1
[356] => 1
[441] => 1
[471] => 1
)
如何打印索引[141]等等?
答案 0 :(得分:7)
使用foreach循环获取
foreach($your_array as $key=>$value) {
echo 'index is '.$key.' and value is '.$value;
}
答案 1 :(得分:1)
使用array_keys获取关联数组的键:
echo implode(', ', array_keys(array(141=>'a', 142=>'b')));
// prints: 141, 142
答案 2 :(得分:1)
如果您已经知道数组索引:
$arrayIndex = 141;
echo $yourarray[$arrayIndex];
或像这样遍历数组:
foreach ($yourarray as $arrayItem) {
echo $arrayItem;
}
或者如果你需要找出数组键/索引:
foreach ($yourarray as $arrayIndex=>$arrayItem) {
echo $arrayIndex." - ". $arrayItem;
}