简体   繁体   中英

Removing Repeating Id Values from 2D array : 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 eg

$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++;
    }

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

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
        )

)

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>';
?>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM