我有这段代码:
<?php
$input = [
["From"=>"Jack", "To"=>"Peter"],
["From"=>"Jack", "To"=>"John"],
["From"=>"Jack", "To"=>"Ali"],
["From"=>"Ali", "To"=>"Jack"],
["From"=>"Ali", "To"=>"Martin"],
["From"=>"Jack", "To"=>"Barmar"],
["From"=>"Peter", "To"=>"John"]
];
$output = [];
array_walk($input, function($element) use (&$output) {
$output[$element['From']][] = $element;
});
print_r(array_values($output));
/*
Array
(
[0] => Array
(
[0] => Array
(
[From] => Jack
[To] => Peter
)
[1] => Array
(
[From] => Jack
[To] => John
)
[2] => Array
(
[From] => Jack
[To] => Ali
)
[3] => Array
(
[From] => Jack
[To] => Barmar
)
)
[1] => Array
(
[0] => Array
(
[From] => Ali
[To] => Jack
)
[1] => Array
(
[From] => Ali
[To] => Martin
)
)
[2] => Array
(
[0] => Array
(
[From] => Peter
[To] => John
)
)
)
现在我想删除To
密钥的唯一时的整个数组(不存在From
和To
密钥的值) 。所以这是预期的结果:(barmar
和martin
并且是唯一的,因此应该删除它们的数组)
/*
Array
(
[0] => Array
(
[0] => Array
(
[From] => Jack
[To] => Peter
)
[1] => Array
(
[From] => Jack
[To] => John
)
[2] => Array
(
[From] => Jack
[To] => Ali
)
)
[1] => Array
(
[0] => Array
(
[From] => Ali
[To] => Jack
)
)
[2] => Array
(
[0] => Array
(
[From] => Peter
[To] => John
)
)
)
我该怎么做?
答案 0 :(得分:0)
将单个数组中的所有值推送,然后计算其元素。如果count> 1,那么只将值推送到输出。试试这个:
$input = [
["From"=>"Jack", "To"=>"Peter"],
["From"=>"Jack", "To"=>"John"],
["From"=>"Jack", "To"=>"Ali"],
["From"=>"Ali", "To"=>"Jack"],
["From"=>"Ali", "To"=>"Martin"],
["From"=>"Jack", "To"=>"Barmar"],
["From"=>"Peter", "To"=>"John"]
];
$exist_array=array();
foreach ($input as $key => $value) {
foreach ($value as $key1 => $value1) {
array_push($exist_array, $value1);
}
}
$vals = array_count_values($exist_array);
$output = [];
array_walk($input, function($element) use (&$output,$vals) {
if($vals[$element['To']] > 1)
$output[$element['From']][] = $element;
});
echo "<pre>";
print_r(array_values($output));
检查结果:
答案 1 :(得分:0)
<?php
echo ('<pre>');
$input = [
["From"=>"Jack", "To"=>"Peter"],
["From"=>"Jack", "To"=>"John"],
["From"=>"Jack", "To"=>"Ali"],
["From"=>"Ali", "To"=>"Jack"],
["From"=>"Ali", "To"=>"Martin"],
["From"=>"Jack", "To"=>"Barmar"],
["From"=>"Peter", "To"=>"John"]
];
$input = array_filter($input, function($from_to) use ($input) {
foreach ($input as $item) {
if ($item == $from_to)
continue;
if (in_array($from_to['To'], $item, true))
return true;
}
return false;
});
$output = [];
array_walk($input, function($element) use (&$output) {
$output[$element['From']][] = $element;
});
print_r(array_values($output));
答案 2 :(得分:0)
计算每个名称的出现次数,然后检查计数为1的$input
名称的值。$output
数组将是array_walk的起点
$input = [
["From"=>"Jack", "To"=>"Peter"],
["From"=>"Jack", "To"=>"John"],
["From"=>"Jack", "To"=>"Ali"],
["From"=>"Ali", "To"=>"Jack"],
["From"=>"Ali", "To"=>"Martin"],
["From"=>"Jack", "To"=>"Barmar"],
["From"=>"Peter", "To"=>"John"]
];
$unique = array();
$output = array();
foreach ($input as $entrie => $row) {
foreach($row as $key => $value ) {
if (! isset($unique[$value])) {
$unique[$value]= 1 ;
} else {
$unique[$value]++ ;
}
}
}
foreach ($input as $entrie => $row) {
if ( $unique[$row['From']] != 1 && $unique[$row['To']] != 1 )
$output[] = $row;
}
print_r($output);
答案 3 :(得分:0)
试试这个
<?php
$input = [
["From"=>"Jack", "To"=>"Peter"],
["From"=>"Jack", "To"=>"John"],
["From"=>"Jack", "To"=>"Ali"],
["From"=>"Ali", "To"=>"Jack"],
["From"=>"Ali", "To"=>"Martin"],
["From"=>"Jack", "To"=>"Barmar"],
["From"=>"Peter", "To"=>"John"]
];
$removeUnique = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($input)),FALSE);
$removeUnique = (array_unique(array_diff_assoc($removeUnique, array_unique($removeUnique))));
$output = [];
array_walk($input, function($element) use (&$output,$removeUnique) {
if(in_array($element['From'], $removeUnique) && in_array($element['To'], $removeUnique) ){
$output[$element['From']][] = $element;
}
});
echo "<pre>";
print_r(array_values($output));
参考链接:
答案 4 :(得分:0)
@stack尝试这个:
<?php
$input = [
["From"=>"Jack", "To"=>"Peter"],
["From"=>"Jack", "To"=>"John"],
["From"=>"Jack", "To"=>"Ali"],
["From"=>"Ali", "To"=>"Jack"],
["From"=>"Ali", "To"=>"Martin"],
["From"=>"Jack", "To"=>"Barmar"],
["From"=>"Peter", "To"=>"John"]
];
echo "<pre>";
print_r($input); // array before
$to = [];
foreach($input as $key => $val){
if(in_array($val["To"], $to)){
unset($input[$key]);
}
$to[] = $val["To"];
}
echo "<pre>";
print_r(array_values($input));// array after