开发者

how to loop and compare two arrays at the same time(PHP)

开发者 https://www.devze.com 2023-04-07 07:14 出处:网络
I have two array. What I want to do is compare the key [\'user_id\'] of two arrays, if they are the same, pass the [\'user_id\'] and [\'ref\'] in hidden form. I tried to put them into two foreach, but

I have two array. What I want to do is compare the key ['user_id'] of two arrays, if they are the same, pass the ['user_id'] and ['ref'] in hidden form. I tried to put them into two foreach, but system seems into a dead lock.

 <?php fore开发者_开发问答ach($_SESSION['printing_set'] as $data) { ?>
   <?php foreach(getProvenaMailableUserlist() as $userlist){ ?>
    <input type="hidden" name="reference[<?php echo $data['user_id'] ?>]" value="<? if($userlist['user_id'] == $data['user_id']){echo $userlist['ref'];}  ?>" />    
   <?php } ?> 
 <?php } ?>

What is the right way to do that?


What you are doing is printing again and again the part of '<input type="hidden" name="...'. here is what you should do

<?php
  echo '<input type="hidden" name="reference[' . $data['user_id'] . ']" value="';  //olny one time.
  foreach($_SESSION['printing_set'] as $data) {
    foreach(getProvenaMailableUserlist() as $userlist){
        if($userlist['user_id'] == $data['user_id']){
            echo $userlist['ref']; //only if condition is true
        }

    }
  }
  echo '" />'; //only one time
 ?>


You've got some funky formatting going on, so it's hard to tell where the error might be. Try it like this:

<?php
foreach($_SESSION['printing_set'] as $data) {
   foreach(getProvenaMailableUserlist() as $userlist){
       $value = "";
       if($userlist['user_id'] == $data['user_id'])
          $value = $userlist['ref'];
       echo "<input type='hidden' name='reference$user_id' value='$value' /> \n"; 
   }
} 
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号