Php 删除重复和原始字符串
我想删除重复字符串和原始重复! 例如:Php 删除重复和原始字符串,php,arrays,duplicates,Php,Arrays,Duplicates,我想删除重复字符串和原始重复! 例如: my string = one two three one two and i want = three 我的代码: <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post"> <p> <textarea name="keywords" rows="20" columns="120"></textarea> </p>
my string = one two three one two
and i want = three
我的代码:
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<textarea name="keywords" rows="20" columns="120"></textarea>
</p>
<p>
<input type="submit" name="submit" />
</p>
</form>
<?php
if(!empty($_POST['keywords']))
{
$posted = $_POST['keywords'];
$posted = array_unique(explode(' ', str_replace("\r\n", ' ', $posted)));
echo print_r($posted, true);
}
?>
将字符串分解成单词后-计算数组中的所有值:
$posted = explode(' ', str_replace("\r\n", ' ', $posted));
$counted_values = array_count_values($posted);
// then filter by value, if value equals 1 - echo it, or do whatever you want
foreach ($counted_values as $k => $v) {
if ($v == 1) {
echo $k;
}
}
与另一个答案类似,但使用的是array\u filter
:
$posted = array_filter(array_count_values(str_replace("\r\n", ' ', $posted)),
function($v) { return $v === 1; });
array\u count\u value
你放弃了吗???