gpt4 book ai didi

php - 如何在php中比较两个mysql表

转载 作者:行者123 更新时间:2023-11-29 14:52:11 25 4
gpt4 key购买 nike

我有一个电影表,我想比较两个用户的常见电影。

$array1=array();$array2=array();$query2="select name from movie where user_id='2'";$result2=mysql_query($query2) or die(mysql_error());while($rss = mysql_fetch_assoc ($result2)){    $array1[]=$rss;}print_r($array1);

这将打印

Array ( [0] => Array ( [name] => Snatch ) [1] => Array ( [name] => The Social Network Movie )
[2] => Array ( [name] => Death Note ) [3] => Array ( [name] => Titanic )
[4] => Array ( [name] => Once Upon a Time in the West ) )

对于第二个用户

$query3="select name from movie where user_id=1";$result3=  mysql_query($query3) or die(mysql_error());while($rss1=  mysql_fetch_assoc($result3)){    $array2[]=$rss1;}print_r($array2);
这将打印

Array ( [0] => Array ( [name] => The Lord of the Rings Trilogy ) [1] => Array ( [name] => Snatch ) 
[2] => Array ( [name] => The Social Network Movie ) [3] => Array ( [name] => Scarface )
[4] => Array ( [name] => Once Upon a Time in the West ) [5] => Array ( [name] => Legend of the Guardians: The Owls of Ga'Hoole ) [6] => Array ( [name] => Once Upon a Time in America )
[7] => Array ( [name] => Butch Cassidy and the Sundance Kid ) [8] => Array ( [name] => Fracture )
[9] => Array ( [name] => Invictus ) [10] => Array ( [name] => Pride and Glory ) [11] => Array ( [name] => Casablanca ) )

当我比较这两个数组时,它给出了第一个数组。

$match= array_intersect($array1, $array2);print_r($match);
结果将是
Array ( [0] => Array ( [name] => Snatch ) [1] => Array ( [name] => The Social Network Movie )
[2] => Array ( [name] => Death Note ) [3] => Array ( [name] => Titanic )
[4] => Array ( [name] => Once Upon a Time in the West ) )

但常见的电影有:《抓捕》,社交网络电影,《西部往事》

最佳答案

尝试编写以下内容


while($rss = mysql_fetch_assoc ($result2))
{
$array1[]=$rss['名称'];
}

这样数组中的值将是字符串并且可以轻松进行比较。我完全不知道 array_intersect 如何处理嵌套数组,但它可以很好地处理字符串。

关于php - 如何在php中比较两个mysql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5509657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com