0

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?

1

3 Answers 3

7

Use foreach loop to get

foreach($your_array as $key=>$value) {
    echo 'index is '.$key.' and value is '.$value;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

Use array_keys to get the keys of an associative array:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.