简体   繁体   English

打印出每个数组元素

[英]Printing out each array element

I have an array and I use print_r and this what happen: 我有一个数组,我使用了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
)

How can I print out the index [141] and so on? 如何打印索引[141]等?

Use foreach loop to get 使用foreach循环获取

foreach($your_array as $key=>$value) {
    echo 'index is '.$key.' and value is '.$value;
}

if you already know the array index: 如果您已经知道数组索引:

$arrayIndex = 141;
echo $yourarray[$arrayIndex];

or loop through the array like this: 或像这样遍历数组:

foreach ($yourarray as $arrayItem) {
echo $arrayItem;
}

or if you need to find out array key/index: 或者如果您需要找出数组键/索引:

foreach ($yourarray as $arrayIndex=>$arrayItem) {
echo $arrayIndex." - ". $arrayItem;
}

Use array_keys to get the keys of an associative array: 使用array_keys获取关联数组的键:

echo implode(', ', array_keys(array(141=>'a', 142=>'b')));
// prints: 141, 142

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM