从2D数组中删除重复Id值:PHP

从2D数组中删除重复Id值:PHP

问题描述:

I have an array like

$a = array(
    array('id'=>1, 'value'=>2),
    array('id'=>2, 'value'=>3),
    array('id'=>3, 'value'=>4),
    array('id'=>1, 'value'=>5),
    array('id'=>5, 'value'=>10),
    array('id'=>2, 'value'=>6),

);

from this i want an array which don't have the repeating id wise keys value e.g

$a = array(
    array('id'=>3, 'value'=>4),
    array('id'=>1, 'value'=>5),
    array('id'=>5, 'value'=>10),
    array('id'=>2, 'value'=>6)
);

i tried the following one but unable to get final result.

$i=0;
    foreach ($a as $key=>$value) {
        // $arr[$i] = $value;
        foreach ($a as $key1=>$value1) {
            if(!empty(array_diff($value, $value1))) {
                if( $value['id'] == $value1['id'] ) {
                    $arr[$i]=$value1;
                }
                else {
                    $arr[$i]=$value;
                }
            }
        }
        $i++;
    }

我有一个数组,如 p>

  $ a = array(  
 array('id'=> 1,'value'=> 2),
 array('id'=> 2,'value'=> 3),
 array('id'=  > 3,'value'=> 4),
 array('id'=> 1,'value'=> 5),
 array('id'=> 5,'value'=  > 10),
 array('id'=> 2,'value'=> 6),
 
); 
  code>  pre> 
 
 

来自 这个我想要一个没有重复id智能键值的数组 e.g p>

  $ a = array(
 array('id'=> 3)  ,'value'=> 4),
 array('id'=> 1,'value'=> 5),
 array('id'=> 5,'value'=> 10  ),
 array('id'=> 2,'value'=> 6)
); 
  code>  pre> 
 
 

我尝试了以下但无法 得到最终结果。 p>

  $ i = 0; 
 foreach($ a as $ key => $ value){
 // $ arr [$ i]  = $ value; 
 foreach($ a as $ key1 => $ value1){
 if(!empty(array_diff($ value,$ value1))){
 if($ value ['id'] =  = $ value1 ['id']){
 $ arr [$ i] = $ value1  ; 
} 
其他{
 $ arr [$ i] = $ value; 
} 
} 
} 
 $ i ++; 
} 
  code>  pre> 
 

This should work for you:

(Here i just simply go trough each innerArray with foreach. Then i get the id from the innerArray and check if it is already in the tmp array and if not i add it to the tmp array. At the end i just simply use array_values(), so that the indexes are clean and starts with 0. The array_reverse() is to get the entire arrray in the right order)

<?php

    $a = array(
            array('id'=>1, 'value'=>2),
            array('id'=>2, 'value'=>3),
            array('id'=>3, 'value'=>4),
            array('id'=>1, 'value'=>5),
            array('id'=>5, 'value'=>10),
            array('id'=>2, 'value'=>6),
    );


    $tmp = array();
    foreach(array_reverse($a) as $v) {
        $id = $v['id'];
        isset($tmp[$id]) or $tmp[$id] = $v;
    }
    $a = array_reverse(array_values($tmp));
    print_r($a);

?>

Output:

Array
(
    [0] => Array
        (
            [id] => 3
            [value] => 4
        )

    [1] => Array
        (
            [id] => 1
            [value] => 5
        )

    [2] => Array
        (
            [id] => 5
            [value] => 10
        )

    [3] => Array
        (
            [id] => 2
            [value] => 6
        )

)

I'd create an array to hold you unique ID values then check against them through your iteration. If the unique ID is in the unique ID array, then remove it.

<?php
$a=array(
    array('id'=>1, 'value'=>2),
    array('id'=>2, 'value'=>3),
    array('id'=>3, 'value'=>4),
    array('id'=>1, 'value'=>5),
    array('id'=>5, 'value'=>10),
    array('id'=>2, 'value'=>6),
);

$unique_ids = array();
foreach($a as $key => $array){

    if(in_array($array['id'],$unique_ids)){
        unset($a[$key]);
    }else{
        $unique_ids[] = $array['id'];
    }

}

echo '<pre>',print_r($a),'</pre>';

Try this, It works fine:

<?php
$a=array(
        array('id'=>1, 'value'=>2),
        array('id'=>2, 'value'=>3),
        array('id'=>3, 'value'=>4),
        array('id'=>1, 'value'=>5),
        array('id'=>5, 'value'=>10),
        array('id'=>2, 'value'=>6),

    );
echo 'Input Array';
echo '<pre>';
print_r($a);  /* it is your inout array */
echo '</pre>';
$storage = array();
$result = array();
foreach($a as $b){

    if(!in_array($b['id'],$storage) ){
        $storage[] = $b['id'];
         $result[] =  array('id'=>$b['id'],'value'=>$b['value']);
    }

}
echo 'Output Array';
echo '<pre>';
print_r($result); /* it is your output array */
echo '</pre>';
?>

Just create an array with id as the key and then throw away those keys when you're done:

$result = [];
foreach ($a as $item) {
    $result[$item['id']] = $item;
}
$result = array_values($result);

You can further reduce that to a one-liner:

$result = array_values(array_combine(array_column($a, 'id'), $a));