Jump to content

[SOLVED] How to compare elements from two arrays


ekestes

Recommended Posts

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>";

Link to comment
Share on other sites

<?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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.