0

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
    Duplicate: http://stackoverflow.com/questions/245509/algorithm-to-tell-if-two-arrays-have-identical-members – bhushya Jul 28 '14 at 04:12
  • is `uid` unique in your `checklist` array? – FuzzyTree Jul 28 '14 at 04:16
  • Bhushya - Thanks for that, I understand that I'm not adding to the dialogue much, hopefully since I wasn't able to grok what sgt illustrated below from the other articles I've been reading, this will help someone else. – BlueDreaming Jul 28 '14 at 10:09

1 Answers1

1

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

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • Thank you. While it was a duplicate as folks above have noted, it was exactly the duplicate that I needed and your answer helped me out a lot. – BlueDreaming Jul 28 '14 at 18:31