What are some potential pitfalls when trying to increment a variable within a PHP loop for JavaScript array creation?

When trying to increment a variable within a PHP loop for JavaScript array creation, one potential pitfall is not properly resetting the variable for each iteration of the loop. This can lead to unexpected results or errors in the resulting JavaScript array. To solve this issue, make sure to reset the variable to its initial value at the beginning of each loop iteration.

// Example of resetting the variable within a PHP loop for JavaScript array creation

$counter = 0;
$js_array = [];

foreach ($php_array as $value) {
    $js_array[$counter] = $value;
    $counter++;
}

// $js_array now contains elements from $php_array with proper incrementing counter