dongnan1989 2014-07-28 04:09
浏览 115
已采纳

Php循环遍历两个数组

I'm posting data from a web page to a php page with Ajax. It's working fine.

I get my data from the $_POST, loop through the values and create an array called checklist.

if($_POST != ''):
$dataset = $_POST['data'];
$checklist = array();
$eventid='';
foreach ($dataset as $i => $row)
{
   $uid = $row['box-id'];
   $state = $row['box-state'] ;
   $eventid = $row['e_id'];
   $checklist[] = array('uid'=>$uid, 
                    'state'=> $state);
}

Checklist has two fields, a uid and a state.

I then run a script that generates another array, called $updates. It loops through a different set of objects and outputs the data to populate the variables for $updates. The structure of $updates is as such.

 $updates[] =  array('uid'=>$uid, 
                     'state'=> $state,
                     'class' => $class,
                     'container' => $button_cont,
                     'closer' => $button_closer);

What I would like to do is to compare $updates with $checklist.

I'd like to know the most efficient way to match the records by the uid and compare the state. If the state matches, I'd like to do nothing.

I've read a few of the articles on looping and search, but I'm thinking I've been looking at this for too long because it's Greek to me. Thanks for assistance.

  • 写回答

1条回答 默认 最新

  • dongyue7796 2014-07-28 04:17
    关注

    save the checklist like -

    $checklist[$uid] = $state;
    

    same for updates

    $updates[$uid] = array('state'=> $state,
                     'class' => $class,
                     'container' => $button_cont,
                     'closer' => $button_closer);
    

    then start the loop

    foreach ($updates as $key => $update) { 
       if ($update['state'] == $checklist[$key]) {
           //your action
       }//compare the values
    }
    

    $key will be the uid.hope it will help you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图