0
array(13) {
    [0]=> string(15) "TEST1"
    [1]=> string(12) "TEST2"
    [2]=> string(12) "TEST3"
    [3]=> string(12) "TEST4"
}

Output should look like this:

0 TEST1
1 TEST2
2 TEST3
...

I'm new to arrays so I'm searching for the cleanest way to do it. It worked with some counter variables but it was ugly as hell. Thanks for your time.

6
  • Take a look at foreach, I'm sure you will get to your solution. Commented Jan 3, 2016 at 20:39
  • Do a foreach loop. Commented Jan 3, 2016 at 20:39
  • Not only foreach u can also use for loop Commented Jan 3, 2016 at 20:45
  • 1
    @devpro, how would you use a for loop? I can only think it's useful for known integer-indexed arrays starting at 0, with sequential keys. Commented Jan 3, 2016 at 21:58
  • See also: stackoverflow.com/questions/3304885/… Commented Jan 3, 2016 at 22:00

1 Answer 1

3

You can use a foreach loop

foreach($array as $key => $value) {
    echo $key . " " . $value . "\n";
}
Sign up to request clarification or add additional context in comments.

4 Comments

Exactly what I was looking for. Didn't know that ($array as $key => $value) is possible. Thanks!
This is the most easy way love this.. u can also use for loop for it @jonas-knobloch
@JonasKnobloch, don't take this the wrong way, but if you didn't know this, you obviously didn't look up the PHP reference, which is the defactor reference to the language. You should spend some time here : php.net/docs.php ( and php.net/manual/en/language.types.array.php )
Yeah. I'm new to PHP and should spend more time with the documentation. My question looks now more dumb every day for me. Have a nice day.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.