2

2 つの配列を比較したいのですが、2 つの配列のサイズが異なる場合があります。

たとえば、フォームがあり、次の値を受け取ります。

// The post send more values than other array:
//   name , email , password, phone , address
// but in other cases send only one value and the other array it's bigger
foreach($_POST as $key=>$value)
{    
    $fields_array[]=$key;    
}

反対側には、比較したい別の配列があります。

    $fields_compare=array("name","email");

この場合、$fields_array という配列の方が大きい場合は問題ありません。ただし、たとえば 2 番目の配列が大きい場合、問題が発生します。

私は続けてこれをしました:

$aa=array_diff($fields_array,$fields_compare);
$bb=array_intersect($fields_array,$fields_compare);

foreach ($aa as $aaa)
{
    // Show the others different values, no show name and email
    print "".$aaa."<br>";
}

foreach ($bb as bbb)
{
    // Show the same Values in this case the same will be name and email ///
    print "".$bbb."<br>";
}

最初の配列が大きい場合、これはすべて機能しますが、それ以外の場合は機能せず、実際の違いは示されません。

4

1 に答える 1