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. Link to comment https://forums.phpfreaks.com/topic/6834-if-the-element-in-array_1-is-not-in-array_2-then-push-that-element-onto-another-new-array-result/ Share on other sites More sharing options...
obsidian Posted April 7, 2006 Share Posted April 7, 2006 [code]$result = array_diff($array1, $array2);[/code] Link to comment https://forums.phpfreaks.com/topic/6834-if-the-element-in-array_1-is-not-in-array_2-then-push-that-element-onto-another-new-array-result/#findComment-24842 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] Link to comment https://forums.phpfreaks.com/topic/6834-if-the-element-in-array_1-is-not-in-array_2-then-push-that-element-onto-another-new-array-result/#findComment-24853 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 Link to comment https://forums.phpfreaks.com/topic/6834-if-the-element-in-array_1-is-not-in-array_2-then-push-that-element-onto-another-new-array-result/#findComment-24856 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. Link to comment https://forums.phpfreaks.com/topic/6834-if-the-element-in-array_1-is-not-in-array_2-then-push-that-element-onto-another-new-array-result/#findComment-24860 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 Link to comment https://forums.phpfreaks.com/topic/6834-if-the-element-in-array_1-is-not-in-array_2-then-push-that-element-onto-another-new-array-result/#findComment-24862 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. Link to comment https://forums.phpfreaks.com/topic/6834-if-the-element-in-array_1-is-not-in-array_2-then-push-that-element-onto-another-new-array-result/#findComment-24888 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.