php - Removing duplicate array, from an array of arrays -
here's example of array i'm working against:
array ( [0] => array ( [id] => 1331 [shortname] => mcs-115-113c [userid] => 663 [email] => asdfasfd@asdfasfd.br [username] => foobarbaz [nombrecompleto] => asdfasdf [lastlogin] => 1367501486 ) [1] => array ( [id] => 1331 [shortname] => mafa-eoof [userid] => 323 [email] => asdfasfd@asdfasfd.br [username] => foobarbaz [nombrecompleto] => asdfasdf [lastlogin] => 136732186 ) [2] => array ( [id] => 1331 [shortname] => mkt-foobar [userid] => 434 [email] => asdfasfd@asdfasfd.br [username] => adsfasdf [nombrecompleto] => asdfasdf [lastlogin] => 1367234486 )
in case, want compare username
element in array , delete duplicates.
so in case, return 2 elements, username foobarbaz
, adsfasdf
:
array ( [0] => array ( [id] => 1331 [shortname] => mafa-eoof [userid] => 323 [email] => asdfasfd@asdfasfd.br [username] => foobarbaz [nombrecompleto] => asdfasdf [lastlogin] => 136732186 ) [1] => array ( [id] => 1331 [shortname] => mkt-foobar [userid] => 434 [email] => asdfasfd@asdfasfd.br [username] => adsfasdf [nombrecompleto] => asdfasdf [lastlogin] => 1367234486 )
how can accomplish in php?
try this:
<?php $test=array ( 0 => array ( 'id' => '1331', 'shortname' => 'mcs-115-113c', 'userid' => '663', 'email' => 'asdfasfd@asdfasfd.br', 'username' => 'foobarbaz', 'nombrecompleto' => 'asdfasdf', 'lastlogin' => '1367501486', ), 1 => array ( 'id' => '1331', 'shortname' => 'mafa-eoof', 'userid' => '323', 'email' => 'asdfasfd@asdfasfd.br', 'username' => 'foobarbaz', 'nombrecompleto' => 'asdfasdf', 'lastlogin' => '136732186' ), 2 => array ( 'id' => '1331', 'shortname' => 'mkt-foobar', 'userid' => '434', 'email' => 'asdfasfd@asdfasfd.br', 'username' => 'adsfasdf', 'nombrecompleto' => 'asdfasdf', 'lastlogin' => '1367234486' ) ); $userdupe=array(); foreach ($test $index=>$t) { if (isset($userdupe[$t["username"]])) { unset($test[$index]); continue; } $userdupe[$t["username"]]=true; } print_r($test); ?>
Comments
Post a Comment