我有以下数组:
array(
[PM-AAA] => Array
(
[codePm] => PM-32249
[codeArt] => Array
(
[0] => 32249
)
[codeArtInFlux] => Array
(
[0] => 123456
)
)
[PM-BBB] => Array
(
[codePm] => PM-32249
[codeArt] => Array
(
[0] => 33270
)
[codeArtInFlux] => Array
(
[0] => 484946
)
)
[PM-CCC] => Array
(
[codePm] => PM-82242
[codeArt] => Array
(
[0] => 82242
[1] => 82245
[2] => 82246
)
[codeArtInFlux] => Array
(
[0] => 5191
[1] => 51949
[2] => 26486
)
)
)
我想保持数组的"codePm"值是唯一的。例如,在上面的数组中,"PM-CCC"数组将被保留,因为"codePm"是唯一的,与"PM-AAA"one_answers"PM-BBB"相反,它们共享相同的"codePm"值。
有可能用一个函数来做吗?
据我所知,没有任何函数可以直接做到这一点,您可以试试:
function insert_unique_key($key_name,$elem,&$array) {
//Get all elements with the key
$values=array()
foreach ($array as $ii) {
$values[]=$ii[$key_name];
}
//Check if the value exists
if (in_array($elem[$key_name], $values)===FALSE) {
$array[]=$elem;
}
}
在你的代码中:
insert_unique_key('codePm',$elem_to_insert,$array_of_elements);
你需要对数组中的每个元素做同样的操作来创建一个新的数组
,
EDIT:对不起,这是为了插入新的唯一值到数组中,而不是为了获取唯一值。
查看注释,我认为您希望只有唯一值。试试这个函数:
function get_uniques_by_subkey($key_name,$array) {
//Get all elements with the key
$values=array();
foreach ($array as $ii) {
$values[]=$ii[$key_name];
}
//Get the elements that only appeared one time
$count=array_count_values($values);
unset($values);$values=array();
foreach ($count as $key => $n) {
if ($n==1)
$values[]=$key;
}
//Get the values
$out=array();
foreach ($array as $key => $value) {
if (in_array($value[$key_name],$values))
$out[$key]=$value;
}
return $out;
}
您可以在这里查看结果:http://codepad.org/QmuoYxsk
是这样的吗?
<?
function removeDuplicatesByKey($a, $k) {
$r = array();
$tmp = array();
foreach ($a as $ind => $arr) {
$elem_found = array_search($arr[$k], $tmp);
if ($elem_found === false) {
$tmp[] = $arr[$k];
$r[$ind] = $arr;
} else {
// ok, element found, need to remove both ..
foreach ($r as $index => $r_arr) {
if ($r_arr[$k] == $arr[$k]) {
unset($r[$index]);
}
}
}
}
return $r;
}
$full_arr = array(
'PM-AAA' => array
(
'codePm' => 'PM-32249',
'codeArt' => array(
'0' => 32249
),
'codeArtInFlux' => array(
'0' => 123456
)
)
,
'PM-BBB' => array
(
'codePm' => 'PM-32249',
'codeArt' => array
(
'0' => 33270
),
'codeArtInFlux' => array
(
'0' => 484946
)
)
,
'PM-CCC' => array
(
'codePm' => 'PM-82242',
'codeArt' => array
(
'0' => 82242,
'1' => 82245,
'2' => 82246
),
'codeArtInFlux' => array
(
'0' => 5191,
'1' => 51949,
'2' => 26486
)
)
);
print_r(removeDuplicatesByKey($full_arr, 'codePm'));
?>
Array
(
[1] => Array
(
[codePm] => PM-82242
[codeArt] => Array
(
[0] => 82242
[1] => 82245
[2] => 82246
)
[codeArtInFlux] => Array
(
[0] => 5191
[1] => 51949
[2] => 26486
)
)
)
我做了这个....在我的工作速度模式....你可以改进它;)
$array = array(
'PM-AAA' => array(
'codePm' => 'PM-32249',
'codeArt' => array(32249),
'codeArtInFlux' => array(123456)
),
'PM-BBB' => array(
'codePm' => 'PM-32249',
'codeArt' => array(33270 ),
'codeArtInFlux' => array(484946)
),
'PM-CCC' => array(
'codePm' => 'PM-82242',
'codeArt' => array(82242,82245,82246),
'codeArtInFlux' => array(5191,51949,26486)
)
);
$code_count = array();
$arr2 = array();
foreach($array as $counter) {
$key = $counter['codePm'];
$code_count[] = $key;
}
$arr2 = (array_count_values($code_count));
print_r($arr2); //now i know how many times my code is repeated
while ($code_name = current($arr2)) {
if ($code_name == 1) {
$unique_code = key($arr2);
}
next($arr2);
}
echo $unique_code."</br>"; //i have my unique code
foreach ($array as $key) {
var_dump($key);
if($key['codePm']==$unique_code)
$arr_aux = $key;
}
echo "I have mi array ready with the unique val ;) </br>";
var_dump($arr_aux);
Saludos;)