0

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

7 Answers 7

1

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).

Sign up to request clarification or add additional context in comments.

Comments

1
$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

3 Comments

So a for loop is the way to go? The users array will always be in the same order as the occurrences. I use array_push from a sql query to insert values into the arrays. They will never be reversed like the example you gave. Regardless, a for loop works. I still don't get why it isn't displaying both users.
@user1852176 There are other ways too, but I would do it "manually", you can't rely on a function if you are not completely familiar with it. I assume $key is a link between user and occurrence, so in this case, order doesn't matter, if you use some array_* function, I'm sure it will.
@user1852176 You reversed arrays like it was said.
0

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

2 Comments

That adds the values of the arrays together so the output is entirely different.
Good point, I modified my answer now that I understand better.
0

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

Comments

0

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

Comments

0

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>';
}

Comments

-1

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.

4 Comments

Because $occurrences.length == length... The if statement is always true. And you're using short tags. I guess your question should probably have been 'Why do'... :P
@BenM what? Please explain what you're getting at, because that doesn't make a lick of sense.
Run your code on a server. echo $occurences.length; outputs the string length. Therefore you're comparing two strings, which are both length.
>.< Attentiveness is key. Answer has been edited. Had my head stuck in Javascript mode. As for the short tags, I used them because the Asker used them. (sorry for time jump. actually had to do work for a moment there) @BenM

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.