PHP isn't returning unique items between assigned_IPs and IP_array. I need an unique output.
I already tried merging arrays and comparing them.
$ips2 = array();
while ($row = mysql_fetch_assoc($result))
{
$ips2[] = $row["ipaddr"];
}
$assigned_ips = $ips2;
$ip_array = explode("
",$ips);
$ips_left = array_diff($ip_array,$assigned_ips);
I need to return an unique array once PHP compares the 2 item lists.
Lucky for you, PHP has an array_unique() function for just such a purpose! You could perform it on both of the compared arrays before array_diff()
, but I am pretty sure best (an most efficient) practice would be to perform it only once when you already have the results:
$ips2 = array();
while ($row = mysql_fetch_assoc($result))
{
$ips2[] = $row["ipaddr"];
}
$assigned_ips = $ips2;
$ip_array = explode("
",$ips);
$ips_left = array_unique(
array_diff($ip_array,$assigned_ips)
);
Just for the sake of completeness, here is an alternative way that could be more efficient if you have a large number of duplicates that would bog down the array_diff()
function:
$ips2 = array();
while ($row = mysql_fetch_assoc($result))
{
$ips2[] = $row["ipaddr"];
}
$assigned_ips = $ips2;
$ip_array = explode("
",$ips);
$ips_left = array_diff(
array_unique($ip_array)
array_unique($assigned_ips)
);