使用 PHP 与 2 个 foreach 循环中的数组进行比较时如何获取迭代计数
How to get iteration count when comparing to arrays in 2 foreach loops using PHP
我有 2 个数组,我想计算它们的总票数。数组一是用户提案,数组二是用户对提案的投票。我正在尝试比较 2 并计算每个提案的总票数。
这是 2 个数组 -
$props = json_decode('[{"proposal":"proposal 1", "owner":"tom", "lock_time":1639440607},{"proposal":"proposal 2", "owner":"bob", "lock_time":1639455554}]', true);
$votes = json_decode('[{"vote":"approve", "owner":"tom", "prop-id":1639440607},{"vote":"reject", "owner":"bob", "prop-id":1639455554},{"vote":"reject", "owner":"tom", "prop-id":1639440607}]', true);
想要的输出如下-
Proposal 1 = 1 Approve and 1 Reject
Proposal 2 = 0 Approve and 1 Reject
我得到的输出是-
Proposal 1 = 1 Approve and 1 Reject
Proposal 2 = 1 Approve and 1 Reject
这是我的尝试,但未提供所需的输出 -
foreach($props as $props){
$lock_time = $props['lock_time'];
$my_proposal = $props['proposal'];
foreach($votes as $votes){
$id = $votes['prop-id'];
$vote = $votes['vote'];
if($lock_time == $id){
if($vote == 'approve'){
$yay++;
}elseif($vote == 'reject'){
$nay++;
}
}
}
echo 'Proposal: '.$my_proposal.'<br>';
echo 'yay:: '.$yay.'<br>';
echo 'nay:: '.$nay.'<br>';
echo '<br><br>';
}
这是我的演示 - http://sandbox.onlinephpfunctions.com/code/abea2fa828c067ce3efc1440af201fce839645b6
在这个街区
if($vote == 'approve'){ $yay++; }elseif($vote == 'reject'){ $nay++;}
您需要为每个提案重置值,因此可能会像这样:
foreach($props as $props){
$lock_time = $props['lock_time'];
$my_proposal = $props['proposal'];
$yay=0;
$nay=0;
foreach($votes as $vote){
$id = $vote['prop-id'];
$theVote = $vote['vote'];
if($lock_time == $id){
if($theVote == 'approve'){
$yay++;
}elseif($theVote == 'reject'){
$nay++;
}
}
}
echo 'Proposal: '.$my_proposal.PHP_EOL;
echo 'yay:: '.$yay.PHP_EOL;
echo 'nay:: '.$nay.PHP_EOL;
echo PHP_EOL;
}
我有 2 个数组,我想计算它们的总票数。数组一是用户提案,数组二是用户对提案的投票。我正在尝试比较 2 并计算每个提案的总票数。
这是 2 个数组 -
$props = json_decode('[{"proposal":"proposal 1", "owner":"tom", "lock_time":1639440607},{"proposal":"proposal 2", "owner":"bob", "lock_time":1639455554}]', true);
$votes = json_decode('[{"vote":"approve", "owner":"tom", "prop-id":1639440607},{"vote":"reject", "owner":"bob", "prop-id":1639455554},{"vote":"reject", "owner":"tom", "prop-id":1639440607}]', true);
想要的输出如下-
Proposal 1 = 1 Approve and 1 Reject
Proposal 2 = 0 Approve and 1 Reject
我得到的输出是-
Proposal 1 = 1 Approve and 1 Reject
Proposal 2 = 1 Approve and 1 Reject
这是我的尝试,但未提供所需的输出 -
foreach($props as $props){
$lock_time = $props['lock_time'];
$my_proposal = $props['proposal'];
foreach($votes as $votes){
$id = $votes['prop-id'];
$vote = $votes['vote'];
if($lock_time == $id){
if($vote == 'approve'){
$yay++;
}elseif($vote == 'reject'){
$nay++;
}
}
}
echo 'Proposal: '.$my_proposal.'<br>';
echo 'yay:: '.$yay.'<br>';
echo 'nay:: '.$nay.'<br>';
echo '<br><br>';
}
这是我的演示 - http://sandbox.onlinephpfunctions.com/code/abea2fa828c067ce3efc1440af201fce839645b6
在这个街区
if($vote == 'approve'){ $yay++; }elseif($vote == 'reject'){ $nay++;}
您需要为每个提案重置值,因此可能会像这样:
foreach($props as $props){
$lock_time = $props['lock_time'];
$my_proposal = $props['proposal'];
$yay=0;
$nay=0;
foreach($votes as $vote){
$id = $vote['prop-id'];
$theVote = $vote['vote'];
if($lock_time == $id){
if($theVote == 'approve'){
$yay++;
}elseif($theVote == 'reject'){
$nay++;
}
}
}
echo 'Proposal: '.$my_proposal.PHP_EOL;
echo 'yay:: '.$yay.PHP_EOL;
echo 'nay:: '.$nay.PHP_EOL;
echo PHP_EOL;
}