0

I have this code:

    <!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>

I basicly want to check if the array is the same as the input from the textarea. This is what the input should be:

Cher Beau Haut Jeune Gros Nouveau Bon Long Vieux Mauvais Autre Joli Petit Grand Large Premier

And the output should be: They're the same. But I did something wrong because it keeps saying: "They're different" Thanks in advance.

Input was wrong excuse me. EDIT:

Beau Haut Jeune Gros Nouveau Bon Long Vieux Mauvais Autre Joli Petit Grand Large Premier Cher

MrDikke
  • 157
  • 1
  • 1
  • 10
  • 1
    See http://stackoverflow.com/questions/901815/php-compare-array – aland Oct 29 '14 at 20:19
  • 1
    `$input = $_POST['textarea']` is a string, not an array. I think you want to compare `$correct` with `$exp` instead. – showdev Oct 29 '14 at 20:20

3 Answers3

1

Your arrays have different internal ordering, which means they're different. Two arrays will only test equal if they have the same number of elements, in the same order, with the same value:

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)

Try running both through sort(), so that (theoretically), they're both in the same order.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Sorry, they should be in the same order on input thats required. My fault. – MrDikke Oct 29 '14 at 20:23
  • textarea just submits as a single string, not an array, so replace/collapse any linebreaks in that string, implode your other array with a space, then compare the strings. – Marc B Oct 29 '14 at 20:25
0

Use in_array - http://php.net/manual/en/function.in-array.php

Loop through your array;

$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;
dojs
  • 497
  • 2
  • 11
0
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 ";
CGeorgian
  • 551
  • 6
  • 10