The foreach
loop is assigning each element of the policyid
array to the $id
variable in turn. It is not being reset so much as re-assigned (to each element of the array you are iterating over).
In order to return an array of only the ids, you will need to collect them and then return the new collection:
public function findPolicyIds($coverageId = null) {
....
$all = array();
foreach($policyid as $id) {
// here we are collecting only the ID value...
$all[] = $id['Policy']['id'];
}
return $all;
}