简体   繁体   English

php循环通过两个数组

[英]Php looping through two arrays

I'm posting data from a web page to a php page with Ajax. 我正在使用Ajax将数据从网页发布到php页面。 It's working fine. 一切正常。

I get my data from the $_POST, loop through the values and create an array called checklist. 我从$ _POST获取数据,遍历值并创建一个名为清单的数组。

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. 清单包含两个字段,一个uid和一个状态。

I then run a script that generates another array, called $updates. 然后,我运行一个脚本,该脚本生成另一个名为$ updates的数组。 It loops through a different set of objects and outputs the data to populate the variables for $updates. 它循环遍历一组不同的对象,并输出数据以填充$ updates的变量。 The structure of $updates is as such. $ updates的结构就是这样。

 $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. 我想做的是将$ updates与$ checklist进行比较。

I'd like to know the most efficient way to match the records by the uid and compare the state. 我想知道通过uid匹配记录并比较状态的最有效方法。 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. 感谢您的协助。

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 $ key将成为uid.hope它将对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM