1

Given:

foreach ($rows as $row) {
    echo $row['id'];
    echo $row['firstname'];
    echo $row['lastname'];
}

How do I get the current index of $rows during any given iteration, without a separate count variable?

What I tried:

foreach ($rows as $key => $row) {
    echo $row['id'];
    echo $row['firstname'];
    echo $row['lastname'];
}

EDIT BELOW

But $key gives me the current index of $row, not the current index of $rows.

What I want to do is:

foreach ($rows as $row) {
    if(current_index($rows) == 0){
        // do something
    }
    echo $row['id'];
    echo $row['firstname'];
    echo $row['lastname'];
}

To my knowledge, PHP doesn't have a current_index() function or something like it.

Here's what $rows looks like:

Array
(
    [0] => Array
        (
            [id] => 1
            [firstname] => Firstname one
            [lastname] => Lastname one
        )

    [1] => Array
        (
            [id] => 2
            [firstname] => Firstname two
            [lastname] => Lastname two
        )

    [2] => Array
        (
            [id] => 3
            [firstname] => Firstname three
            [lastname] => Lastname three
        )
)
15
  • 1) Have you echo'ed $key in the loop and are you sure it is not what you want? 2) Please make a little example with an array what you get now as output and what you expected to get Commented Oct 15, 2015 at 17:30
  • This seems to be unclear But this gives me the current index of $row, not the current index of $rows. Commented Oct 15, 2015 at 17:31
  • 3
    @Exception throw new Exception("Unclear question"); Commented Oct 15, 2015 at 17:32
  • @Rizier123 check out my edits Commented Oct 15, 2015 at 17:38
  • 2
    @FastTrack No, $key is the current index of $rows, which you are iterating. ((Otherwise make an example of the current output + expected output with the shown example array)) Commented Oct 15, 2015 at 17:45

3 Answers 3

2

You are referencing the array value as an array. That's why it is giving you $row['id']

You need to use $key as that is the value of your current index in the $rows array.

When you use foreach($rows as $key => $row) this is what it is practically doing:

  • $rows - input array to loop through
  • $key - key of your current location in the first dimensional array
  • $row - value of your current location in the first dimensional array
Sign up to request clarification or add additional context in comments.

3 Comments

What if array contains string keys ?
I this he is asking for integer keys like 1,2,3 and not like string indexes
It will still work for string indexes. It just won't be a 0, 1, 2.. etc. It would be the Associative Index.
1

Well $key will give current array key which may be string so I think this following will be possible solution

$i=0;
foreach ($rows as $key => $row) { 
echo $row['id']; 
echo $row['firstname']; 
echo $row['lastname']; 
echo $i;
$i++;
}

1 Comment

As stated in my question, I'd like to accomplish this without using a separate count variable.
0

Use associative array

$rows = array("id"=>"id","firstname"=>"firstname","lastname"=>"lastname")
foreach ($rows as $key=>$row) {
    if($key == "firstname"){ 
        // do something
    }
}

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.