我有这个代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Frans</title>
</head>
<body>
<form method="POST">
<textarea name="textarea" cols="16" rows="4" wrap="OFF"/>
</textarea><input type="submit" name="submit" value="submit">
</form><pre><?php
if(isset($_POST['submit'])){
if(!empty($_POST['textarea'])) {
$exp = array_filter(explode("'n", $_POST['textarea']));
print_r($exp);
// Add DB Insert here
}
$correct = array(
'Beau',
'Haut',
'Jeune',
'Gros',
'Nouveau',
'Bon',
'Long',
'Vieux',
'Mauvais',
'Autre',
'Joli',
'Petit',
'Grand',
'Large',
'Premier',
'Cher',
);
$input = $_POST['textarea'];
echo ($correct == $input) ? 'they''re same' : 'they''re different';
print_r($correct);
}
?>
</body>
</html>
我基本上想检查数组是否与文本区域的输入相同。这就是输入应该是什么:
雪儿BeauHautJeuneGros新运动Bon长的VieuxMauvaisAutreJoliPetit壮丽的大的Premier
结果应该是:它们是一样的。但我做错了,因为它一直在说:"他们不一样"提前谢谢。
对不起,输入错误。编辑:
BeauHautJeuneGros新运动Bon长的VieuxMauvaisAutreJoliPetit壮丽的大的总理Cher
您的数组有不同的内部排序,这意味着它们是不同的。只有当两个数组具有相同数量的元素、相同的顺序和相同的值时,它们才会测试为相等:
php > $x = array('a', 'b');
php > $y = array('b', 'a');
php > $z = array('a', 'b');
php > var_dump($x == $y);
bool(false)
php > var_dump($x == $z);
bool(true)
试着通过sort()
运行两者,这样(理论上)它们的顺序相同。
使用in_array
-http://php.net/manual/en/function.in-array.php
循环通过您的阵列;
$exp = explode("/n", $_POST['textarea']);
for ($i = 0; $i < count($exp); $i++)
{
if (in_array($exp[$i], $correct))
{
$output = "They're the same";
}
else
{
$output = "They're different";
break;
}
}
echo $output;
if (count ($array1) == count ($array2) ) //have same size
{
$identical = 1; //we assume both are identical and some item is different will become 0
for ($i=0; $i <count ($array1) ; i++ )
{
if ($array1[$i] != $array2[$i] )
$identical = 0;
}
if ($identical == 1 )
echo "arra1 is identical with array2 ,all items is same order";
else
echo "arra1 is different form array2 ";
}
else
echo "arra1 is diffeent form array2 ";