3
Array
(
    [0] => Array
        (
            [user_id] => 78
            [post_id] => 3
            [post_user_added_id] => 2
        )

    [1] => Array
        (
            [user_id] => 76
            [post_id] => 8
            [post_user_added_id] => 16
        )

    [2] => Array
        (
            [user_id] => 78
            [post_id] => 9
            [post_user_added_id] => 12
        )

    [3] => Array
        (
            [user_id] => 76
            [post_id] => 9
            [post_user_added_id] => 15
        )

     [4] => Array
         (
             [user_id] => 77
             [post_id] => 9
             [post_user_added_id] => 15
         )

)

The idea here is that when there is a duplicate user_id, it will just display only one? This is the expected result:

 Array
 (
      [2] => Array
          (
             [user_id] => 78
             [post_id] => 9
             [post_user_added_id] => 12
           )

        [3] => Array
            (
                [user_id] => 76
                [post_id] => 9
                [post_user_added_id] => 15
            )

         [4] => Array
             (
                 [user_id] => 77
                 [post_id] => 9
                 [post_user_added_id] => 15
             )

    )

The reason why [2] key instead of [0] key or [1] key instead of [3] is displayed because I want to get the bottom key of the duplicate key. It's kinda hard to explain but I hope you understood the scenario or the output that I expected.

Your help would be greatly appreciated! Thanks! :)

5
  • What have you tried so far? - Please post your code and make clear with which point exactly you've hit the road-block. Commented Apr 24, 2012 at 15:35
  • I didn't try anything since I really don't know the logic on performing my expected output. Commented Apr 24, 2012 at 15:35
  • This website is for programmers, so please see the FAQ which questions to ask here. Commented Apr 24, 2012 at 15:36
  • Have you considered moving your arrays into a database? Commented Apr 24, 2012 at 15:37
  • 2
    I'm a programmer, that's why I came up with that PHP array values. I'm just asking how to remove duplicate values on an array. Commented Apr 24, 2012 at 15:38

4 Answers 4

8

Try this :

foreach($arr as $k => $v) 
{
    foreach($arr as $key => $value) 
    {
        if($k != $key && $v['user_id'] == $value['user_id'])
        {
             unset($arr[$k]);
        }
    }
}
print_r($arr);
Sign up to request clarification or add additional context in comments.

1 Comment

The previous one that you left was working perfectly! no need to array_reverse() it since I want to get the bottom key of the duplicate key value. :)
1

Try

$array = Array (
        "0" => Array (
                "user_id" => 78,
                "post_id" => 3,
                "post_user_added_id" => 2 
        ),

        "1" => Array (
                "user_id" => 76,
                "post_id" => 8,
                "post_user_added_id" => 16 
        ),

        "2" => Array (
                "user_id" => 78,
                "post_id" => 9,
                "post_user_added_id" => 12 
        ),

        "3" => Array (
                "user_id" => 76,
                "post_id" => 9,
                "post_user_added_id" => 15 
        ),

        "4" => Array (
                "user_id" => 77,
                "post_id" => 9,
                "post_user_added_id" => 15 
        ) 
);
$keys = array ();

// Get Position
foreach ( $array as $key => $value ) {
    $keys [$value ['user_id']] = $key;
}

// Remove Duplicate
foreach ( $array as $key => $value ) {
    if (! in_array ( $key, $keys )) {
        unset ( $array [$key] );
    }
}
var_dump ( $array );

Output

array
  2 => 
    array
      'user_id' => int 78
      'post_id' => int 9
      'post_user_added_id' => int 12
  3 => 
    array
      'user_id' => int 76
      'post_id' => int 9
      'post_user_added_id' => int 15
  4 => 
    array
      'user_id' => int 77
      'post_id' => int 9
      'post_user_added_id' => int 15

1 Comment

You might need to array_reverse the initial array, since that is not the desired output.
0

You can use a foreach loop to overwrite the user_id. I think something like this should work

$new = array();
foreach ($array as $value)
{
    $new[$value['user_id']] = $value;
}
print_r($new);

Comments

0

Following worked for me, for cleaning the following:

$arr = array(

    array("ID"=>"234"),
    array("ID"=>"235"),
    array("ID"=>"236"),
    array("ID"=>"236"),
    array("ID"=>"234"),
    );

Code:

for ($i=0; $i <count($arr) ; $i++) {

    $distinct_id = $arr[$i]["ID"]; // this is the ID for which we want to eliminate the duplicates
    $position = $i; // position of that array (so we ignore it)
    $unset_arr = array(); // these duplicate entries will be removed

    // Collect list of duplicates to remove
    for ($y=0; $y <count($arr) ; $y++) {

        // Skip the position of the array that we're checking (we want to remove duplicate not the original)
        if($y == $position)
            continue;

        // If found remove and reset keys
        if($arr[$y]["ID"] == $distinct_id) {
            $unset_arr[] = $y;
        }

    }

    // Remove IDs collected in the loop above
    foreach ($unset_arr as $k) {
        unset($arr[$k]);
    }

    // Reset keys
    $arr = array_values($arr);

}

(reverse the array if you want to remove newer duplicates instead of older ones)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.