0

我有以下数组。

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”和“PM-BBB”相反,它们共享相同的“codePm”' s 值。

有可能用一个功能来做吗?

4

3 回答 3

0

据我所知,没有任何功能可以直接执行此操作,您可以试试这个:

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);

而且您必须将数组的每个元素都放入一个新数组中

--

编辑:对不起,这是为了在数组中插入新的唯一值,而不是获取唯一值。

查看评论,我认为您只需要唯一值试试这个功能:

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

于 2013-03-01T13:35:27.793 回答
0

像这样的东西?

 <?

    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
                )

        )

)
于 2013-03-01T13:36:06.117 回答
0

I made this....on speed mode in my work....you can improve it ;)

    $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 ;)

于 2013-03-01T14:20:44.057 回答