I’m trying to display 2 arrays with a foreach loop, but for some reason when the values in the arrays are set to 0, only the last item of the array is displaying
Say I have the following array values:
users array ( 0 => user1, 1 => user2)
occurrences array ( 0 => 0, 1 => 3) //the key represents the user from the users array
The output of the foreach loop will display correctly
//output
user1 0
user2 3
However, if both values are 0 only user2 will be displayed
users array ( 0 => user1, 1 => user2)
occurrences array ( 0 => 0, 1 => 0); //the key represents the user from the users array
//output (should also display user1 0)
user2 0
This is my foreach loop.
?>
<table>
<th>User</th><th># of Occurrences</th>
<?
foreach (array_combine($occurrences, $users) as $occur => $user){
?>
<tr><td><? echo $user; ?></td><td><? echo $occur; ?></td></tr>
<?
}
?></table>
<?
The code in the question is performing the following:
I would imagine you are after the opposite behaviour:
Try swapping
$occurrencesand$usersin the call, i.e.,array_combine($users, $occurrences)The reason you are only seeing user2 is because array_combine considers the entries
0 => 0 and 1 => 0and will receive0 0as a key list. Therefore, it can only produce a single key in the resulting array hash (it is using the values from the occurrences array to build the key list).