Array comparison question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • phpCodeHead

    Array comparison question

    Hello fellow codemeisters!

    I am needing to parse through two lists of serial numbers for parts
    being received into an inventory database. First, is the list of
    serial numbers already in the database (these were provided from mfg
    and sucked into db via xml web service); the second is a list that
    will be populated into a textarea field, via barcode scanning, on the
    receiving form during the receiving process (to us, at run time).
    Prior to inerting the data, I am needing to display, in a color-coded
    fashion, any serial numbers being received that were not provided by
    the mfg; as well as the serial numbers being received in that were not
    provided by the mfg already. I then need to compare and have say, for
    simplicity sake, all matched in "green"; all that were in db but not
    received in "red"; and all received in but on in db in "yellow".

    I am thinking of using array_diff(). ( believe me, there is a
    question coming!! :-) )

    The code looks like this:

    <?php
    // in my real app:
    // $array1 is pulled from the db; these are serial numbers for a
    single lineitem to be received in on a PO
    // $array2 is pulled from the db as well; these are the provided
    serial numbers from the xml
    $array1 = array("snFromPO " =100234, 100345, 100456, 100567,
    100678);
    $array2 = array("snFromXM L" =100234, 100345, 100456, 100678,
    100789);
    $result = array_diff($arr ay1, $array2);
    $result2 = array_diff($arr ay2, $array1);

    foreach ( $result AS $diff ) {
    echo 'Serial numbers receiving not in database: <div
    class="caution" >' . $diff . '</div>, ';
    }
    echo '<br />';

    foreach ( $result2 as $diff ) {
    echo 'Serial numbers in database not receiving in: <div
    class="nogo">' . $diff . '</div>, ';
    }
    echo '<br />';
    ?>

    As you may have guessed, this returns:

    Serial numbers receiving not in database: 100567,
    Serial numbers in database not receiving in: 100789,

    Finally, the question: As you can see I am passing the two arrays into
    array_diff() twice; once to get the diff between $array1 and $array2;
    the second to get the diff between $array2 and $array1.

    Is this the best approach to take to accomplish my goal? Is there a
    better way of doing this?

    I appreciate any and all comments, suggestions, and put downs. :-)

    Thanks all,

    Gene Kelley
    PHP Developer
    Falkor Group, LLC
    Chicago, IL, USA

  • Schraalhans Keukenmeester

    #2
    Re: Array comparison question

    phpCodeHead wrote:
    Hello fellow codemeisters!
    >
    I am needing to parse through two lists of serial numbers for parts
    being received into an inventory database. First, is the list of
    serial numbers already in the database (these were provided from mfg
    and sucked into db via xml web service); the second is a list that
    will be populated into a textarea field, via barcode scanning, on the
    receiving form during the receiving process (to us, at run time).
    Prior to inerting the data, I am needing to display, in a color-coded
    fashion, any serial numbers being received that were not provided by
    the mfg; as well as the serial numbers being received in that were not
    provided by the mfg already. I then need to compare and have say, for
    simplicity sake, all matched in "green"; all that were in db but not
    received in "red"; and all received in but on in db in "yellow".
    >
    I am thinking of using array_diff(). ( believe me, there is a
    question coming!! :-) )
    >
    The code looks like this:
    >
    <?php
    // in my real app:
    // $array1 is pulled from the db; these are serial numbers for a
    single lineitem to be received in on a PO
    // $array2 is pulled from the db as well; these are the provided
    serial numbers from the xml
    $array1 = array("snFromPO " =100234, 100345, 100456, 100567,
    100678);
    $array2 = array("snFromXM L" =100234, 100345, 100456, 100678,
    100789);
    $result = array_diff($arr ay1, $array2);
    $result2 = array_diff($arr ay2, $array1);
    >
    foreach ( $result AS $diff ) {
    echo 'Serial numbers receiving not in database: <div
    class="caution" >' . $diff . '</div>, ';
    }
    echo '<br />';
    >
    foreach ( $result2 as $diff ) {
    echo 'Serial numbers in database not receiving in: <div
    class="nogo">' . $diff . '</div>, ';
    }
    echo '<br />';
    ?>
    >
    As you may have guessed, this returns:
    >
    Serial numbers receiving not in database: 100567,
    Serial numbers in database not receiving in: 100789,
    >
    Finally, the question: As you can see I am passing the two arrays into
    array_diff() twice; once to get the diff between $array1 and $array2;
    the second to get the diff between $array2 and $array1.
    >
    Is this the best approach to take to accomplish my goal? Is there a
    better way of doing this?
    >
    I appreciate any and all comments, suggestions, and put downs. :-)
    >
    Thanks all,
    >
    Gene Kelley
    PHP Developer
    Falkor Group, LLC
    Chicago, IL, USA
    >
    Looks sound and elegant to me. Way faster than looping manually through
    the arrays.
    Note the result arrays may have empty 'cells' in them. Apparantly both
    array_merge() and array_values() strip the empty ones from your
    resulting arrays, the latter is said to be significantly faster. (not
    tested by me, so don't kill me if I'm wrong).

    Have you got any test results indicating how long the given operations
    will take on seriously long arrays? From the description of the issue at
    hand I reckon they can become quite large in a realistic application.

    GL
    Sh

    Comment

    • Simon Stienen

      #3
      Re: Array comparison question

      You could array_intersect + array_unique before array_diffing to get the
      part numbers that are in common first. This speeds up array_diff, since it
      has to compare fewer items. However, as you have to array_intersect (+
      array_unique) first, overall performance might even be decreased.

      Another approach might be to array_flip the common numbers instead off
      array_uniqueing them (since flipping does unique them anyway), then looping
      the arrays yourself using foreach or array_filter and check for the
      existance of he number using isset($common_f lipped[$number]). I'm not into
      PHPs array interna, but this might use a search tree (as it's an index
      lookup) and therefore a complexity of O(log(n)) per item, while I suspect
      array_diff to iterate through the items, therefore having a complexity of
      O(n). AIMB: I'm not into the interna, but it might be faster at large tile
      lists.

      Comment

      Working...