jayx412
jayx412

Reputation: 1

Printing out each array element

I have an array and I use print_r and this what happen:

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?

Upvotes: 0

Views: 19447

Answers (3)

Awais Qarni
Awais Qarni

Reputation: 18006

Use foreach loop to get

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

Upvotes: 7

bhu1st
bhu1st

Reputation: 1318

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;
}

Upvotes: 2

phihag
phihag

Reputation: 287775

Use array_keys to get the keys of an associative array:

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

Upvotes: 1

Related Questions