Rather than using array_push()
, since you intend to set the array index directly from $group['Group']['ID']
, use that value as the index to $groups_array
in the assignment:
$groups_array = array();
foreach ($groups as $group) {
// assign a new key $group['Group']['ID'] directly
$groups_array[$group['Group']['ID']] = $group['Group']['Name'];
}
Here's what went wrong:
The "reduced by one" issue you describe was sort of a coincidence of having users with ids 1, 2
appending to an array. The first was appended at index 0
and the second as 1
. If the array started non-empty, the users would have been appended to the end of it.
When you did
array_push($groups_array, $group['Group']['ID']=$group['Group']['Name']);
...PHP managed to assign the Name
key into the existing ID
key, overwriting the ID
with the name, and then return that value into the second parameter of array_push()
. So really what PHP eventually saw was
array_push($groups_array, 'Admin');
array_push($groups_array, 'User');