|
Posted by Schraalhans Keukenmeester on 04/03/07 00:01
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("snFromXML" => 100234, 100345, 100456, 100678,
> 100789);
> $result = array_diff($array1, $array2);
> $result2 = array_diff($array2, $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
Navigation:
[Reply to this message]
|