Re: array_unique by id multi-dimensional array
| From: | sivaji j.g | Date: | Fri, 28 Oct 2011 08:18:25 +0000 |
| Subject: | Re: array_unique by id multi-dimensional array | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-315480@lists.php.net to get a copy of this message | ||
On Fri, Oct 28, 2011 at 1:22 PM, Tontonq Tontonq <rootdot@gmail.com> 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] => Ÿóø_݃tÝ
> %ÆŽVË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@gmail.com
Gtalk / Skype: sivaji2009
>
>