PHP - Optimise code that does a reconciliation between two arrays based on numerous fields











up vote
0
down vote

favorite












I build two arrays (I can't use SQL for this as I am using ROQL qhich only allows for very basic SELECT statements as shown) from a SQL query.



I then want to see if any row matches in Array B based off X columns between Array A and Array B



So in the simplified example below, it does a "FULL" match if both the forename and surname exist in a row. It returns "SURNAME" match if just the surname, and "FORENAME" match if it is just the forename that exists.



My question is whether I am using the right functions - how could this be optimised? Returning a filtered array seems too much where I just want to see if that combination of values exists across multiple columns



CODE



$a = array();
$b = array();

RNCPHPConnectAPI::getCurrentContext()->ApplicationContext = "Get contacts";
$res = RNCPHPROQL::query("SELECT Contact.Name.First,Contact.Name.Last FROM CONTACT WHERE disabled=0 LIMIT 500")->next();
while($r = $res->next()) {
try {
$a_inner = array();
$a_inner["forename"] = $r['First'];
$a_inner["surname"] = $r['Last'];
$a = $a_inner;
}
catch ( Exception $err ){
echo "<p><b>Exception</b>: line ".__LINE__.": ".$err->getMessage()."</p>";
}

}
$res = RNCPHPROQL::query("SELECT Contact.Name.First,Contact.Name.Last FROM CONTACT WHERE disabled=1 LIMIT 20000")->next();
while($r = $res->next()) {
try {
$b_inner = array();
$b_inner["forename"] = $r['First'];
$b_inner["surname"] = $r['Last'];
$b = $b_inner;
}
catch ( Exception $err ){
echo "<p><b>Exception</b>: line ".__LINE__.": ".$err->getMessage()."</p>";
}
}

foreach ($a as $k => $v) {

$n = $a['forename'];
$e = $a['surname'];

$full_match = array_filter($b, function($val) use($n, $e){
return ($val['forename']==$n AND $val['surname']==$e);
});

echo $v['forename'] . ' ' . $v['surname'] . ' - ';

if (!empty($full_match)) {
echo 'FULL MATCH<br>';
//return false;
continue;
}

$surname_match = array_filter($b, function($val) use($e){
return ($val['surname']==$e);
});

if (!empty($surname_match)) {
echo 'SURNAME MATCH<br>';
//return false;
continue;
}

$forname_match = array_filter($b, function($val) use($n){
return ($val['forename']==$n);
});

if (!empty($forname_match)) {
echo 'FORENAME MATCH<br>';
//return false;
continue;
}
echo '<br>';
}









share|improve this question


























    up vote
    0
    down vote

    favorite












    I build two arrays (I can't use SQL for this as I am using ROQL qhich only allows for very basic SELECT statements as shown) from a SQL query.



    I then want to see if any row matches in Array B based off X columns between Array A and Array B



    So in the simplified example below, it does a "FULL" match if both the forename and surname exist in a row. It returns "SURNAME" match if just the surname, and "FORENAME" match if it is just the forename that exists.



    My question is whether I am using the right functions - how could this be optimised? Returning a filtered array seems too much where I just want to see if that combination of values exists across multiple columns



    CODE



    $a = array();
    $b = array();

    RNCPHPConnectAPI::getCurrentContext()->ApplicationContext = "Get contacts";
    $res = RNCPHPROQL::query("SELECT Contact.Name.First,Contact.Name.Last FROM CONTACT WHERE disabled=0 LIMIT 500")->next();
    while($r = $res->next()) {
    try {
    $a_inner = array();
    $a_inner["forename"] = $r['First'];
    $a_inner["surname"] = $r['Last'];
    $a = $a_inner;
    }
    catch ( Exception $err ){
    echo "<p><b>Exception</b>: line ".__LINE__.": ".$err->getMessage()."</p>";
    }

    }
    $res = RNCPHPROQL::query("SELECT Contact.Name.First,Contact.Name.Last FROM CONTACT WHERE disabled=1 LIMIT 20000")->next();
    while($r = $res->next()) {
    try {
    $b_inner = array();
    $b_inner["forename"] = $r['First'];
    $b_inner["surname"] = $r['Last'];
    $b = $b_inner;
    }
    catch ( Exception $err ){
    echo "<p><b>Exception</b>: line ".__LINE__.": ".$err->getMessage()."</p>";
    }
    }

    foreach ($a as $k => $v) {

    $n = $a['forename'];
    $e = $a['surname'];

    $full_match = array_filter($b, function($val) use($n, $e){
    return ($val['forename']==$n AND $val['surname']==$e);
    });

    echo $v['forename'] . ' ' . $v['surname'] . ' - ';

    if (!empty($full_match)) {
    echo 'FULL MATCH<br>';
    //return false;
    continue;
    }

    $surname_match = array_filter($b, function($val) use($e){
    return ($val['surname']==$e);
    });

    if (!empty($surname_match)) {
    echo 'SURNAME MATCH<br>';
    //return false;
    continue;
    }

    $forname_match = array_filter($b, function($val) use($n){
    return ($val['forename']==$n);
    });

    if (!empty($forname_match)) {
    echo 'FORENAME MATCH<br>';
    //return false;
    continue;
    }
    echo '<br>';
    }









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I build two arrays (I can't use SQL for this as I am using ROQL qhich only allows for very basic SELECT statements as shown) from a SQL query.



      I then want to see if any row matches in Array B based off X columns between Array A and Array B



      So in the simplified example below, it does a "FULL" match if both the forename and surname exist in a row. It returns "SURNAME" match if just the surname, and "FORENAME" match if it is just the forename that exists.



      My question is whether I am using the right functions - how could this be optimised? Returning a filtered array seems too much where I just want to see if that combination of values exists across multiple columns



      CODE



      $a = array();
      $b = array();

      RNCPHPConnectAPI::getCurrentContext()->ApplicationContext = "Get contacts";
      $res = RNCPHPROQL::query("SELECT Contact.Name.First,Contact.Name.Last FROM CONTACT WHERE disabled=0 LIMIT 500")->next();
      while($r = $res->next()) {
      try {
      $a_inner = array();
      $a_inner["forename"] = $r['First'];
      $a_inner["surname"] = $r['Last'];
      $a = $a_inner;
      }
      catch ( Exception $err ){
      echo "<p><b>Exception</b>: line ".__LINE__.": ".$err->getMessage()."</p>";
      }

      }
      $res = RNCPHPROQL::query("SELECT Contact.Name.First,Contact.Name.Last FROM CONTACT WHERE disabled=1 LIMIT 20000")->next();
      while($r = $res->next()) {
      try {
      $b_inner = array();
      $b_inner["forename"] = $r['First'];
      $b_inner["surname"] = $r['Last'];
      $b = $b_inner;
      }
      catch ( Exception $err ){
      echo "<p><b>Exception</b>: line ".__LINE__.": ".$err->getMessage()."</p>";
      }
      }

      foreach ($a as $k => $v) {

      $n = $a['forename'];
      $e = $a['surname'];

      $full_match = array_filter($b, function($val) use($n, $e){
      return ($val['forename']==$n AND $val['surname']==$e);
      });

      echo $v['forename'] . ' ' . $v['surname'] . ' - ';

      if (!empty($full_match)) {
      echo 'FULL MATCH<br>';
      //return false;
      continue;
      }

      $surname_match = array_filter($b, function($val) use($e){
      return ($val['surname']==$e);
      });

      if (!empty($surname_match)) {
      echo 'SURNAME MATCH<br>';
      //return false;
      continue;
      }

      $forname_match = array_filter($b, function($val) use($n){
      return ($val['forename']==$n);
      });

      if (!empty($forname_match)) {
      echo 'FORENAME MATCH<br>';
      //return false;
      continue;
      }
      echo '<br>';
      }









      share|improve this question













      I build two arrays (I can't use SQL for this as I am using ROQL qhich only allows for very basic SELECT statements as shown) from a SQL query.



      I then want to see if any row matches in Array B based off X columns between Array A and Array B



      So in the simplified example below, it does a "FULL" match if both the forename and surname exist in a row. It returns "SURNAME" match if just the surname, and "FORENAME" match if it is just the forename that exists.



      My question is whether I am using the right functions - how could this be optimised? Returning a filtered array seems too much where I just want to see if that combination of values exists across multiple columns



      CODE



      $a = array();
      $b = array();

      RNCPHPConnectAPI::getCurrentContext()->ApplicationContext = "Get contacts";
      $res = RNCPHPROQL::query("SELECT Contact.Name.First,Contact.Name.Last FROM CONTACT WHERE disabled=0 LIMIT 500")->next();
      while($r = $res->next()) {
      try {
      $a_inner = array();
      $a_inner["forename"] = $r['First'];
      $a_inner["surname"] = $r['Last'];
      $a = $a_inner;
      }
      catch ( Exception $err ){
      echo "<p><b>Exception</b>: line ".__LINE__.": ".$err->getMessage()."</p>";
      }

      }
      $res = RNCPHPROQL::query("SELECT Contact.Name.First,Contact.Name.Last FROM CONTACT WHERE disabled=1 LIMIT 20000")->next();
      while($r = $res->next()) {
      try {
      $b_inner = array();
      $b_inner["forename"] = $r['First'];
      $b_inner["surname"] = $r['Last'];
      $b = $b_inner;
      }
      catch ( Exception $err ){
      echo "<p><b>Exception</b>: line ".__LINE__.": ".$err->getMessage()."</p>";
      }
      }

      foreach ($a as $k => $v) {

      $n = $a['forename'];
      $e = $a['surname'];

      $full_match = array_filter($b, function($val) use($n, $e){
      return ($val['forename']==$n AND $val['surname']==$e);
      });

      echo $v['forename'] . ' ' . $v['surname'] . ' - ';

      if (!empty($full_match)) {
      echo 'FULL MATCH<br>';
      //return false;
      continue;
      }

      $surname_match = array_filter($b, function($val) use($e){
      return ($val['surname']==$e);
      });

      if (!empty($surname_match)) {
      echo 'SURNAME MATCH<br>';
      //return false;
      continue;
      }

      $forname_match = array_filter($b, function($val) use($n){
      return ($val['forename']==$n);
      });

      if (!empty($forname_match)) {
      echo 'FORENAME MATCH<br>';
      //return false;
      continue;
      }
      echo '<br>';
      }






      performance php array






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 21 hours ago









      pee2pee

      19810




      19810



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208118%2fphp-optimise-code-that-does-a-reconciliation-between-two-arrays-based-on-numer%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208118%2fphp-optimise-code-that-does-a-reconciliation-between-two-arrays-based-on-numer%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin