Manipulating foreach counter

Here’s my code:

$itemandsize_ids = ‘1,26’;

$ids = explode(‘,’,$itemandsize_ids);

$in2 = “”;

foreach ($ids as $i => $item) {
$key = “:id”.$i;
$in2 .= “$key,”;
$in_params2[$key] = $item; // collecting values into key-value array
}

It produces:
$in_params2 = array(2) { [“:id0”]=> string(1) “1” [“:id1”]=> string(2) “26” }

I need:
$in_params2 = array(2) { [“:id1”]=> string(1) “1” [“:id2”]=> string(2) “26” }

I can do this with a while loop.

Can it be done with a foreach loop?
I haven’t figured out how to manipulate the counter to start at 1 instead of 0.

$key = “:id”.($i+1);

DUH!

Pls excuse for answering my own question.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.