7

私が取り組んでいる配列の例を次に示します。

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
    )

私の場合、username配列内の要素を比較して重複を削除したいと考えています。

したがって、この場合、usernameFOOBARBAZとの 2 つの要素のみを返し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
    )

PHPでこれを達成するにはどうすればよいですか?

4

5 に答える 5

11

これは、 を使用すると簡単に実現できます (内部であるため迅速です) array_unique()SORT_REGULARただし、デフォルトでは、この関数はすべてを文字列にキャストするため、定数を渡す必要があります。(デモ)

<?php
$data = array(
    array(
        'id' => 0,
        'title' => 'Abc'
    ),
    array(
        'id' => 2,
        'title' => 'Def',
        'content' => 'Stackoverflow!'
    ),
    array(
        'id' => 0,
        'title' => 'Abc'
    )
);

var_dump(array_unique($data, SORT_REGULAR));
于 2013-05-05T02:55:26.867 に答える
0
/* here is your array */
$array = 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'
    )
);

/* initializing an array to store usernames to compare */
$userNames = array();
/* looping through array */
foreach($array as $key=>$value){
    if(!empty($userNames) && in_array($value['username'],$userNames)) unset($array[$key]);  //unset from $array if username already exists
    $userNames[] = $value['username'];  // creating username array to compare with main array values
}

/* print updated array */
echo "<pre>";print_r($array);echo "</pre>";
于 2013-05-05T12:11:40.623 に答える