ekestes Posted January 24, 2008 Share Posted January 24, 2008 I have 2 arrays with data from mysql queries. For each item in the first array, I want to take one action if the item exists in the second array, and another action if not. The order of the first array needs to be preserved (so I'm iterating through it once in the proper order). It seems like this should be pretty straightforward, but I'm missing some critical piece of the logic. I know I need to nest my looping through the arrays somehow, but can't quite get it. I can't seem to get all of the items in array2 compared for each element of array1. Hopefully the code will explain the issue more clearly. The way it's written below, it only loops through array2 once (for the first item in array1). $query1 = "SELECT hostname FROM systems order by hostname ASC"; $result1 = mysql_query($query1); $query2 = "SELECT hostname FROM links WHERE changeid = '$id'"; $result2 = mysql_query($query2); echo "<br>Hostname:<br>"; echo "<select name=\"hostname[]\" size=5 multiple>"; while ($row1 = mysql_fetch_assoc($result1)) { while ($row2 = mysql_fetch_assoc($result2)) { # If a link exists, pre-select the hostname if ($row1['hostname'] == $row2['hostname']) { echo "<option value=".$row1['hostname']." selected>".$row1['hostname']."</option>"; } # If not, echo in the other (unselected) hostnames else { echo "<option value=".$row1['hostname'].">".$row1['hostname']."</option>"; } } } echo "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/87569-solved-how-to-compare-elements-from-two-arrays/ Share on other sites More sharing options...
rhodesa Posted January 24, 2008 Share Posted January 24, 2008 <?php //Get Links first $links = array(); $query = "SELECT DISTINCT hostname FROM links WHERE changeid = '$id'"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) $links[] = $row['hostname']; //Build Select $query = "SELECT hostname FROM systems ORDER BY hostname ASC"; $result = mysql_query($query); echo "<br>Hostname:<br>"; echo "<select name=\"hostname[]\" size=5 multiple>"; while ($row = mysql_fetch_assoc($result)) { if (in_array($row['hostname'],$links)) { // If a link exists, pre-select the hostname echo "<option value=".$row1['hostname']." selected>".$row1['hostname']."</option>"; } else { // If not, echo in the other (unselected) hostnames echo "<option value=".$row1['hostname'].">".$row1['hostname']."</option>"; } } echo "</select>"; ?> There is probably a way to do it with pure SQL, where your result would the hostname and the number of entries in the links table for that hostname, but this is the PHP version... Quote Link to comment https://forums.phpfreaks.com/topic/87569-solved-how-to-compare-elements-from-two-arrays/#findComment-447888 Share on other sites More sharing options...
ekestes Posted January 24, 2008 Author Share Posted January 24, 2008 THANK YOU! This is exactly what I needed. Such a simple issue, but things like this can take you forever to figure out when you don't have any guidance. Quote Link to comment https://forums.phpfreaks.com/topic/87569-solved-how-to-compare-elements-from-two-arrays/#findComment-447932 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.