1

Possible Duplicate:
How to remove duplicate values from a multi-dimensional array in PHP

Sorry for writing this too fast, I was working..., sorry. Now I think is good explained.I want remove subarrays with the subelement [title] common, equals, I want only one element, not repetitions, how can I do that?. I made it, but I think must be a way more elegante to make it. I tried with this code:

static function remove_duplicates_titles($deals){
       $result = array();
       $deal_repeated=false;
       foreach ($deals as $index=>$deal) {
           foreach ($deals as $deal_2) {
               //discover if the subarray has the element title repeated is repeated or not
               if ($deal['title'] == $deal_2['title']){ 
                    $deal_repeated=true;
                    unset($deal_2);  
               }
               else{ 
                    $deal_repeated=false;                          
                }

           }
          //if the array has no the element (title) repeated with another....
          if(!$deal_repeated){
                   $result[]=$deal;
           }
       }

       return $result;

}

Array
(
[0] => Array
    (
        [id] => abc
        [title] => bbb
    )

[1] => Array
    (
        [id] => ghi
        [title] => aaa
    )

[2] => Array              //I should to remove this subarray (or the other, only one of both)
    (
        [id] => mno
        [title] => pql     //this is common
    )

[3] => Array
    (
        [id] => abc
        [title] => def
    )

[4] => Array
    (
        [id] => ghi
        [title] => mmm
    )

[5] => Array             //I should to remove this subarray (or the other), only one of both
    (
        [id] => mno
        [title] => pql   //this is common
    )

)

Community
  • 1
  • 1
itaka
  • 399
  • 1
  • 5
  • 16
  • 1
    `must be a way more elegante to make it` More elegant than what? Have tried anything so far to solve your problem? – feeela Oct 22 '12 at 11:54
  • Are you trying to remove all duplicated or get unique values ?? and you add your desired output – Baba Oct 22 '12 at 11:55
  • As mentioned, post your code if you say you have got it working already. How can things be improved if we don't know the baseline? Also, your example seems off: as you depict, elements 1 and 4 have `pql` in common. But, elements 0 and 2 are also similar. Should they be removed as well? All in all, your question can use some improvements. – jro Oct 22 '12 at 12:05

2 Answers2

5

This function will keep the FIRSTS elements it finds and remove any duplicate based on the key you give

$arr = Array (
    Array ( 'id' => 'abc', 'title' => 'bbb' ),
    Array ( 'id' => 'ghi', 'title' => 'aaa' ),
    Array ( 'id' => 'mno', 'title' => 'pql' ),
    Array ( 'id' => 'abc', 'title' => 'def' ),
    Array ( 'id' => 'ghi', 'title' => 'mmm' ),
    Array ( 'id' => 'ere', 'title' => 'pql' )
);
function arrayUniqueFromKey(array $arr,$key)
{
    $titles = array();$ret = array();
    foreach ($arr as $v) {
        if (!in_array($v[$key],$titles)) {
            $titles[] = $v[$key];
            $ret[] = $v;
        }
    }
    return $ret;
}
print_r(arrayUniqueFromKey($arr,'title'));

http://codepad.org/Y3gsibRM

Touki
  • 7,465
  • 3
  • 41
  • 63
  • wow..............really is very elegant your code. Thanks a lot. Really I liked it very much – itaka Oct 22 '12 at 13:16
1
    foreach($array as $key => $val) {
     if (is_array($val)) { 
      foreach($array as $key2 => $val2) {
       if($val['id']==$val2['id'] || $val['title']==$val2['title'])
       {
         unset($array[$key]);
         break;
       }
      }
    }
  }
Arun Killu
  • 13,581
  • 5
  • 34
  • 61