user1852176
user1852176

Reputation: 455

combined foreach not displaying all array values in PHP

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>
<?

Upvotes: 0

Views: 1441

Answers (7)

joequincy
joequincy

Reputation: 1395

Why not do

if(count($occurrences)==count($users)){
    foreach($occurences as $k, $v){
        ?><tr><td><? echo $users[$k]; ?></td><td><? echo $v; ?></td></tr><?
    }
}

Confirms the size of both arrays match, then achieves the desired output.

Upvotes: -1

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

$users = array(1 => 'user2', 0 => 'user1');
$occurences = array(0 => 0, 1 => 3);

$count = count($users);
$result = array();
for($i = 0; $i < $count; $i++)
{
  $result[$users[$i]] = $occurences[$i];
}
var_dump($result);

Notice $users array order. Using array_combine, you would get wrong values in this case.

array (size=2)
  'user1' => int 0
  'user2' => int 3

I can't reproduce what you are saying:

var_dump(array_combine([0 => 'user_1', 1 => 'user_2'], [0 => 0, 1 => 0]));

Result:

array (size=2)
  'user_1' => int 0
  'user_2' => int 0

Upvotes: 1

plasmid87
plasmid87

Reputation: 1471

The code in the question is performing the following:

For each occurrence value key, provide a user.

I would imagine you are after the opposite behaviour:

For each user key, provide an occurrence value

Try swapping $occurrences and $users in 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 => 0 and will receive 0 0 as 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).

Upvotes: 1

BenM
BenM

Reputation: 53198

PHP's combining the values in $occurances for use as the array key in the resulting combined array. Since you have specified 0 as the value twice, user2 will always assume the first position in the array.

For your solution, would this not be better:

foreach($occurances as $userindex => $occurs)
{
    echo '<tr><td>'.$users[$userindex].'</td><td>'.$occurs.'</td></tr>';
}

Upvotes: 0

jcjr
jcjr

Reputation: 1503

if both values are 0

then, you have two same keys for two different values and you overwrite first value with the second value; therefore, in the array, there is just one value

Upvotes: 0

Matanya
Matanya

Reputation: 6346

Why are you using an associative array if your keys are merely following numbers?

this worked for me:

<?php 
$user = array('user1','user2');
$occur = array(0,0);

foreach (array_combine($user, $occur) as $key => $value) {
    echo $key . " : " . $value;
}
 ?>

Upvotes: 0

Aaron Hathaway
Aaron Hathaway

Reputation: 4315

How about trying to do it in some fashion like this:

<?php
foreach($users as $userId => $userName) {
 if(isset($occurrences[$userId])) {
?>
  <tr><td><?php echo $userName; ?></td><td><?php echo $occurrences[$userId]; ?></td></tr>
<?php
}
?>

Upvotes: 0

Related Questions