为什么 array_diff 在输入表单中的数组时表现不同?
Why does array_diff act differently when array from an input form?
我有这个输入表格:
<form method="POST" action="unique_value_processor.php">
<textarea cols="50" rows="8" name="usedurls"></textarea>
<textarea cols="50" rows="8" name="freshurls"></textarea>
<textarea cols="50" rows="8" name="filteredurls"></textarea>
<input type="SUBMIT" value="SUBMIT">
处理表单的文件是
$old_urls_exploded = explode("\n", $_POST['usedurls']);
$new_urls_exploded = explode("\n", $_POST['freshurls']);
$arraydiff = array_diff($new_urls_exploded, $old_urls_exploded);
print_r($arraydiff);
所以当我在表格中输入以下内容时:
框 1 (old_urls_exploded):
blue, yellow
框 2 (new_urls_exploded):
yellow, blue, banana
那么它应该 return 只有:
banana
但是array_diff returns:
yellow, banana
然后当您手动键入数组时:
$old_urls_exploded = array('blue','yellow');
$new_urls_exploded = array('yellow','blue','banana');
然后 array_diff return 仅:
banana
应该的..
为什么表单会影响 array_diff 的行为方式?我做错了什么吗?
如@Rizier123 所述,问题是输入表单时添加了空格。
因此使用以下方法解决了问题:
$arraydiff = array_diff(array_map("trim", $new_urls_exploded), array_map("trim", $old_urls_exploded));
谢谢
我有这个输入表格:
<form method="POST" action="unique_value_processor.php">
<textarea cols="50" rows="8" name="usedurls"></textarea>
<textarea cols="50" rows="8" name="freshurls"></textarea>
<textarea cols="50" rows="8" name="filteredurls"></textarea>
<input type="SUBMIT" value="SUBMIT">
处理表单的文件是
$old_urls_exploded = explode("\n", $_POST['usedurls']);
$new_urls_exploded = explode("\n", $_POST['freshurls']);
$arraydiff = array_diff($new_urls_exploded, $old_urls_exploded);
print_r($arraydiff);
所以当我在表格中输入以下内容时:
框 1 (old_urls_exploded):
blue, yellow
框 2 (new_urls_exploded):
yellow, blue, banana
那么它应该 return 只有:
banana
但是array_diff returns:
yellow, banana
然后当您手动键入数组时:
$old_urls_exploded = array('blue','yellow');
$new_urls_exploded = array('yellow','blue','banana');
然后 array_diff return 仅:
banana
应该的..
为什么表单会影响 array_diff 的行为方式?我做错了什么吗?
如@Rizier123 所述,问题是输入表单时添加了空格。
因此使用以下方法解决了问题:
$arraydiff = array_diff(array_map("trim", $new_urls_exploded), array_map("trim", $old_urls_exploded));
谢谢