JustinK101 Posted April 7, 2006 Share Posted April 7, 2006 Hello,I have two full arrays of varied size:$array_1$array_2How can I write code to compare every element in $array_1 to $array_2?I need to do: If the element in $array_1 is NOT in $array_2 then push that element onto another new array $result. Clearly then, if the element in $array_1 is in $array_2 then do nothing, i.e. do not push onto $result.Thanks for the advice. Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 7, 2006 Share Posted April 7, 2006 [code]$result = array_diff($array1, $array2);[/code] Quote Link to comment Share on other sites More sharing options...
JustinK101 Posted April 7, 2006 Author Share Posted April 7, 2006 Ok now I need to run a querry on each serial in the array result. I have code, but I am certain its not going to work.[code]$db_name = "yjpalmer_inventoryManager"; mysql_select_db($db_name); foreach($array_result as $current_serial) { $sql_3 = "SELECT id, type_of_device, serial, condition_of_device, associated_order_id, associated_customer_name, UNIX_TIMESTAMP(date_shipped) as sdate FROM inventory WHERE isAssociated = 'Yes' AND serial = '" . $current_serial . "' ORDER BY " . $sort . " " . $ascendOrDescend . ""; $result_3 = mysql_query($sql_3); }[/code]How do I build up an array of these results in a row to output? [b]Acutally I think I may need to do AND serial = array_result[0] OR serial = array_result[1] OR serial = array_result[2] ....[/b] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 7, 2006 Share Posted April 7, 2006 What are you going to do with the results of the query?[code]<?php foreach($array_result as $current_serial) { $sql_3 = "SELECT id, type_of_device, serial, condition_of_device, associated_order_id, associated_customer_name, UNIX_TIMESTAMP(date_shipped) as sdate FROM inventory WHERE isAssociated = 'Yes' AND serial = '" . $current_serial . "' ORDER BY " . $sort . " " . $ascendOrDescend . ""; $result_3 = mysql_query($sql_3);//// you need to fetch the results and do something with them// $row = mysql_fetch_assoc($result_3); $echo '<pre>' . print_r($row,true) . '</pre>'; }?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
JustinK101 Posted April 7, 2006 Author Share Posted April 7, 2006 Ken,Yeah I display a table and output, but I dont want to do all my HTML code in the foreach loop. I think I need to run only 1 sql query and build up my query like:... WHERE serial = '" . $result[0] . "' OR serial = '" . $result[1] . "' OR serial = '" . $result[2] . "' OR serial = '" . $result[X] . "' ...What is the best way of doing this? My result array size isunknown so I cant just write out each combination, plus there are AROUND a few thousand elements in the array so that would'nt be fun. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 7, 2006 Share Posted April 7, 2006 You could build a query like that, but if you have thousands of entries, you will end up with a very long query.[code]<?php//// output the start of your table//$sql_3 = "SELECT id, type_of_device, serial, condition_of_device, associated_order_id, associated_customer_name, UNIX_TIMESTAMP(date_shipped) as sdate FROM inventory WHERE isAssociated = 'Yes' AND serial = '" . implode("' OR serial = '", $result) . "' ORDER BY " . $sort . " " . $ascendOrDescend . "";$result_3 = mysql_query($sql_3);while ($row = mysql_fetch_assoc($result_3)){//// output a row of your table//}//// output the end of your table//?>[/code]Doing it the original way is equivalent to stringing the "OR" clauses[code]<?php//// output the start of your table// foreach($array_result as $current_serial) { $sql_3 = "SELECT id, type_of_device, serial, condition_of_device, associated_order_id, associated_customer_name, UNIX_TIMESTAMP(date_shipped) as sdate FROM inventory WHERE isAssociated = 'Yes' AND serial = '" . $current_serial . "' ORDER BY " . $sort . " " . $ascendOrDescend . ""; $result_3 = mysql_query($sql_3); $row = mysql_fetch_assoc($result_3);//// output a row of your table// }//// output the end of your table//?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
JustinK101 Posted April 7, 2006 Author Share Posted April 7, 2006 Ken,Thanks, yeah the implode worked well. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.