Re: array_unique by id multi-dimensional array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Oct 28, 2011 at 1:22 PM, Tontonq Tontonq <rootdot@xxxxxxxxx> wrote:

> let me explain again
> this is my array
> Array
> (
> [0] => Array
> (
> [likes] => 137846
> [name] => blabla
> [access_token] => AAABZBD
> [id] => 157644197612598
> [link] => http://www.facebook.com/blabla
> )
>
> [1] => Array
> (
> [likes] => 137698
> [name] => blabla
> [access_token] => AAAnnL
> [id] => 157644197612598
> [link] => http://www.facebook.com/blabla
> )
>
> [2] => Array
> (
> [likes] => 134391
> [name] => sieaq
> [access_token] => AAA04
> [id] => 163744743683729
> [link] => http://www.facebook.com/zart
> )
>
>
> [3] => Array
> (
> [name] => defolaq
> [access_token] => AAAZD
> [id] => 139820007610
> [link] => http://www.facebook.com/apps/application.php?id=239820007640
> [likes] => 0
> )
>
> [4] => Array
> (
> [name] => falan
> [access_token] => AAW4NNGAZDZD
> [id] => 374971333999
> [link] => http://www.facebook.com/apps/application.php?id=374951333399
> [likes] => 0
> )
>
> [5] => Array
> (
> [name] => filan
> [access_token] => AAABZCTCw
> [id] => 237184173662497
> [link] => http://www.facebook.com/pages/filan/237184173662497
> [likes] => 0
> )
>
> [6] => Array
> (
> [name] => bigit
> [access_token] => Jj77twGFvS8YyB
> [id] => 10497735291210
> [link] => http://www.facebook.com/pages/bigit/10497735291210
> [likes] => 0
> )
> )
>
> but as you see there are 2 sub array with [id] =>  157644197612598
> i want to delete sub arrays repeated with same id.?
>

If my understanding is right, a simple foreach loop and unset() will do
this. Below is the untested version of snippet I would use to remove the
duplicate sub arrays,

$id = array();
foreach ($items as $key => $item) {
  if (in_array($item['id'], $id)) {
    unset($items[$key]);
  }
  else {
    $id[] = $item['id'];
  }
}


-- 
Sivaji
http://sivaji.drupalgardens.com
@sivaji_ganesh <https://twitter.com/#%21/sivaji_ganesh> in twitter
sivaji2009@xxxxxxxxx
Gtalk / Skype: sivaji2009


>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux