我的php脚本:
$cont = array();
$slq_get_previous_country_list = "SELECT country FROM aw_countries_v2 WHERE id ='11120'";
$result_previous_country_list = mysql_query($slq_get_previous_country_list);
while($row = mysql_fetch_assoc($result_previous_country_list))
{
$cont[] = $row['country'];
}
print_r($cont);
$not_data = array();
$new_contry = array("Greece",
"Israel",
"Macedonia - The Frm Yugoslav Rep Of",
"Malta",
"India");
foreach($cont as $c)
{
if(in_array($c, $new_contry))
{
echo $c."\n";
}
else
{
$not_data[] = $c;
}
}
print_r($not_data);
$cont
数组:
Array
(
[0] => Belgium
[1] => Cyprus
[2] => Czech Republic
[3] => Fiji
[4] => Finland
[5] => Greece
[6] => Iceland
[7] => Ireland
[8] => Israel
[9] => Macedonia - The Frm Yugoslav Rep Of
[10] => Malaysia
[11] => Malta
[12] => Monaco
[13] => Poland
[14] => South Africa
[15] => Austria
)
$not_data
数组:
Array
(
[0] => Belgium
[1] => Cyprus
[2] => Czech Republic
[3] => Fiji
[4] => Finland
[5] => Iceland
[6] => Ireland
[7] => Malaysia
[8] => Monaco
[9] => Poland
[10] => South Africa
[11] => Austria
)
结果:
array(
[0] => austria
);
我做错了什么?
答案 0 :(得分:2)
你的字符串中有空格,in_array
函数是空格和区分大小写的,所以尝试将其添加为前体:
while($row = mysql_fetch_assoc($result_previous_country_list))
{
$cont[] = trim($row['country']);
}
您可能还想尝试strtolower
来比较两个数组的字符串值,而不必担心。
此外,建议不要使用MySQL_,而是要开发 MySQLi ,甚至 PDO 。