我有这个数组。在此我想跳过反向数据的值。
mysqli_set_charset($connection, 'utf8');
$string = "INSERT INTO pass_words(site_name,user_id,pass_id,email,question,answer) VALUE ("
.
' \' '.$site.' \' '
. ','.
' \' '
.$acc_name.' \' '
. ','.
' \' '
.$pass.' \' '
. ','.
' \' '
.$email.' \' '
. ','.
' \' '
.$question.' \' '
. ','
.' \' '
. ''.$answer.' \' '.')' ;
if($query = mysqli_query($connection, mysqli_escape_string($connection, $string))){
echo '<div style="color: #00FF99; margin: 0 0 20px 0;"> The Account Information has been added correctly.</div>';
}
我想跳过这些值:
$data = array(
array(2, 1),
array(1, 2),
array(1, 2),
array(2, 1),
array(2, 3),
array(3, 2),
array(4, 5)
);
我希望结果数组是这样的:
array(1,2)
array(1,2)
array(2,1)
array(3,2)
答案 0 :(得分:2)
$data = [[2,1], [1,2], [1,2], [2,1], [2,3], [3,2], [4,5]];
$data = array_map(
function($value) {
sort($value);
return serialize($value);
},
$data
);
$data = array_unique($data);
$data = array_map('unserialize', $data);
array_walk($data, 'sort');
var_dump($data);
修改强>
根据arbogast的评论,这可以进一步简化,消除序列化/反序列化步骤:
$data = [[2,1], [1,2], [1,2], [2,1], [2,3], [3,2], [4,5]];
array_walk($data, 'sort');
$data = array_unique($data, SORT_REGULAR);
var_dump($data);
答案 1 :(得分:0)
试试这个:
function filterArray($data)
{
$result = array();
array_push($result, $data[0]); //You can always add first item without performing any check
for($i=1; $i<count($data); $i++)
{
$allowAdd = true;
foreach($result as $res)
{
if(($res == $data[$i]) || ($res[0] = $data[$i][1] && $res[1] = $data[$i][0]))
{
$allowAdd = false;
break;
}
}
if($allowAdd)
array_push($result, $data[$i]);
}
return $result;
}
现在,当您致电filterArray($data)
时,会返回过滤后的结果。