cleary1981 Posted September 1, 2008 Share Posted September 1, 2008 hi, I have two arrays. The first is a list of all records. The second is a list of records that meet a certain condition. I need to create an array of the first array - the second array. Heres my code. <?php require "config.php"; // array of all projects $array1 = mysql_query("SELECT DISTINCT proj_id FROM object") or die(mysql_error()); while($all = mysql_fetch_array($array1)) { echo $all['proj_id'] . " "; } // array of projects needing priced $array2 = mysql_query("SELECT DISTINCT proj_id FROM object, module WHERE object.module_name = module.module_name AND confirmed = '0'") or die(mysql_error()); while($notPriced = mysql_fetch_array($array2)) { echo $notPriced['proj_id'] . " "; } ?> Link to comment https://forums.phpfreaks.com/topic/122196-find-the-difference-of-two-arrays/ Share on other sites More sharing options...
burn1337 Posted September 1, 2008 Share Posted September 1, 2008 $array3 = new array([0] => $all[], [1] => $notpriced[]); thats what I would suggest then a for statement or something of the sort Link to comment https://forums.phpfreaks.com/topic/122196-find-the-difference-of-two-arrays/#findComment-630843 Share on other sites More sharing options...
cleary1981 Posted September 1, 2008 Author Share Posted September 1, 2008 Im getting an error "Parse error: syntax error, unexpected T_ARRAY, expecting T_STRING or T_VARIABLE or '$' in C:\wamp\www\approved.php on line 16" <?php require "config.php"; // array of all projects $array1 = mysql_query("SELECT DISTINCT proj_id FROM object") or die(mysql_error()); $all = mysql_fetch_array($array1); //echo $all['proj_id'] . " "; // array of projects needing priced $array2 = mysql_query("SELECT DISTINCT proj_id FROM object, module WHERE object.module_name = module.module_name AND confirmed = '0'") or die(mysql_error()); $notPriced = mysql_fetch_array($array2); //echo $notPriced['proj_id'] . " "; $array3 = new array([0] => $all[], [1] => $notpriced[]); //line 16 while($rest = mysql_fetch_array($array3)) { echo $rest; } ?> Link to comment https://forums.phpfreaks.com/topic/122196-find-the-difference-of-two-arrays/#findComment-630851 Share on other sites More sharing options...
JasonLewis Posted September 1, 2008 Share Posted September 1, 2008 I'm not sure if I follow you correctly. But something like this? <?php require "config.php"; // array of all projects $query1 = mysql_query("SELECT DISTINCT proj_id FROM object") or die(mysql_error()); while($all = mysql_fetch_array($array1)) { $array1[] = $all['proj_id']; } // array of projects needing priced $query2 = mysql_query("SELECT DISTINCT proj_id FROM object, module WHERE object.module_name = module.module_name AND confirmed = '0'") or die(mysql_error()); while($notPriced = mysql_fetch_array($query2)) { $array2[] = $notPriced['proj_id']; } $diff = array_diff($array1, $array2); echo "<pre>"; print_r($diff); echo "</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/122196-find-the-difference-of-two-arrays/#findComment-630857 Share on other sites More sharing options...
burn1337 Posted September 1, 2008 Share Posted September 1, 2008 ok now I see what your triying to do sorry I'm very tired and my brain is half dead atm lol. uhh I would suggest doing a query that joins the two tables, if thats how you are trying to get it... if your wanting to get the one that are priced then change confirmed to 1?? or what it would be if it is priced.. and you would get the same result without the while your not using any conditions so you should lol. or you could even try what projectfear said that would work better then what I said originally lmao Link to comment https://forums.phpfreaks.com/topic/122196-find-the-difference-of-two-arrays/#findComment-630859 Share on other sites More sharing options...
cleary1981 Posted September 1, 2008 Author Share Posted September 1, 2008 I have used your method project fear. I still have a little problem, you may be able to help me. Heres the code. <?php require "config.php"; // array of all projects $query1 = mysql_query("SELECT DISTINCT proj_id FROM object") or die(mysql_error()); while($all = mysql_fetch_array($query1)) { $array1[] = $all['proj_id']; } // array of projects needing priced $query2 = mysql_query("SELECT DISTINCT proj_id FROM object, module WHERE object.module_name = module.module_name AND confirmed = '0'") or die(mysql_error()); while($notPriced = mysql_fetch_array($query2)) { $array2[] = $notPriced['proj_id']; } $diff = array_diff($array1, $array2); while (list ($key, $val) = each ($diff)) { // echo "$val <br>"; $q2 = mysql_query("SELECT project_name, company_name, proj_id FROM project WHERE proj_id = '$val'") or die(mysql_error()); $row = mysql_fetch_array($q2); echo $row['company_name'] . '$' . $row['project_name'] . '$' . $row['proj_id'] . '^'; } ?> this gives the output Big Company$yyy$77^$$^Small Company$project mayhem$111^Small Company$another$222^ You should be able to notice the empty record $$^. How do I get rid of this? if i echo $val i get the following 77 1 111 222 **there are no records in my table with a proj_id = 1 Link to comment https://forums.phpfreaks.com/topic/122196-find-the-difference-of-two-arrays/#findComment-630926 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.