26

以下のコード行を使用して、データベース内のテーブルをループします。

$items_thread = $connection -> fetch_all($sql);

そして、配列を出力すると、次のようになります。

print_r($items_thread);

私はこれを取得します:

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => info@xx.com
        )

    [1] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => info@xx.com
        )

    [2] => Array
        (
            [RecipientID] => 1
            [RecipientScreenname] => Lau T
            [RecipientFirstname] => TK
            [RecipientEmail] => lau@xx.co.uk
        )

)

しかし、配列内の重複するアイテムを削除したいので、array_unique

print_r(array_unique($items_thread));

以下の奇妙な結果が得られますが、これは私が探しているものではありません。

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => info@xx.com
        )

)

理想的には、これを返す必要があると思います。

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => info@xx.com
        )

    [1] => Array
        (
            [RecipientID] => 1
            [RecipientScreenname] => Lau T
            [RecipientFirstname] => TK
            [RecipientEmail] => lau@xx.co.uk
        )

)

それを正しくするために私は何をすべきですか?間違ったPHP構文/デフォルト関数を使用しましたか?

4

9 に答える 9

67

関数がこれarray_uniqueを行います。SORT_REGULARフラグを追加する必要があります。

$items_thread = array_unique($items_thread, SORT_REGULAR);

ただし、brenが示唆しているように、可能であればSQLでこれを行う必要があります。

于 2011-02-18T00:29:19.610 に答える
7

SQLクエリで重複を除外する方がはるかに良いでしょう。UNIQUErecipientIDをフェッチする制約を追加します

于 2011-02-18T00:28:32.780 に答える
4

重複する値を削除するには、array_unique()関数を使用できます。その機能は、配列を受け入れ、重複する値なしで別の配列を返すことです。

array_unique()の一般的な説明を以下に示します。

General Format = array array_unique(array $array [, int $sort_flags=sort_string] )
Parameters     = array: Input array ->sort_flags:
    The second optional parameter is used to compare items as follows
         1. SORT_REGULAR - Normal
         2. SORT_NUMERIC - Numerically
         3. SORT_STRING - As
         4. string  SORT_LOCALE_STRING - As string, based on the current locale.
Return Value   = Another array without duplicate values 

例1:

<?php
$array=array('cricket'=>11,'football'=>11,'chess'=>2);
echo"<br/><b>Initially the values of \$array is:</b><br/>";
var_dump($array);
echo"<br/><b>After removing the duplicates:</b><br/>";
print_r(array_unique($array));
?>

出力:

Initially the values of $array is:
array(3) {
  ["cricket"] => int(11)
  ["football"] => int(11)
  ["chess"] => int(2)
}

After removing the duplicates:
Array
(
    [cricket] => 11
    [chess] => 2
)
于 2012-07-03T05:00:04.660 に答える
1

これを試して:

$data = array_map('unserialize', array_unique(array_map('serialize', $data)));

以下を出力します。

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => info@xx.com
        )

    [2] => Array
        (
            [RecipientID] => 1
            [RecipientScreenname] => Lau T
            [RecipientFirstname] => TK
            [RecipientEmail] => lau@xx.co.uk
        )
)

しかし、これをデータベースに実装する必要があるとも思います。また、私の他の答えと解決策を確認してください。

于 2011-02-18T01:00:53.630 に答える
1

このコードを使用できます。お役に立てば幸いです。それは完全に機能しています:

$array = array(1,1,2,3,4,4,4,5);
$temp=array();

for($i=0; $i<=count($array); $i++)
 {

    if($array[$i] != '')
    {
        for($j=$i+1; $j<=count($array); $j++ )
        {
            if($array[$i]==$array[$j])
            {
                $array[$j] = '';
            }
            else
            {
                $temp[$array[$i]]=$array[$i];
            }

         }

    }
}

print_r($temp); 
于 2016-09-29T13:22:46.507 に答える
0

これを実現するには、通常のphp配列を使用できます。

$newArray = array();
foreach ($origArray as $user)
{
   $newArray[$user['RecipientID']] = $user;
}
于 2011-02-18T00:30:12.880 に答える
0

$ unique = array_keys(array_flip($ array));

より高速で、さらに配列キーインデックスをリセットします。

ソース: http: //php.net/manual/en/function.array-unique.php#89519

これは単純な配列の場合のみです。

于 2015-05-02T15:23:18.750 に答える
0

以下のコードを確認してください。これがあなたの助けになることを願っています。

$resultArray = uniqueAssocArray($actualArray, 'RecipientID');

function uniqueAssocArray($array, $uniqueKey) {
if (!is_array($array)) {
    return array();
}
$uniqueKeys = array();
foreach ($array as $key => $item) {
    $groupBy=$item[$uniqueKey];
    if (isset( $uniqueKeys[$groupBy]))
    {
        //compare $item with $uniqueKeys[$groupBy] and decide if you 
        //want to use the new item
        $replace= false; 
    }
    else
    {
        $replace=true;
    }
    if ($replace) $uniqueKeys[$groupBy] = $item;   
}
return $uniqueKeys;

}

于 2015-07-10T06:46:51.707 に答える
0
$res1       = mysql_query("SELECT * FROM `luggage` where r_bus_no='".$tour_id."' and `date1`='$date' AND `login_id`='".$_SESSION['login_id']."'");
}
/// create a array to store value
$city_arr   = array();
if(mysql_num_rows($res1)>0)
{
    while($row1 = mysql_fetch_array($res1))
    {

/// insert the value in array use the array_push function
            array_push($city_arr,$row1['r_to']);
    }

//// remove the duplicate entry in array use the array_unique function
    $a          = array_unique($city_arr);
    echo "<option value='' selected='selected'> -- Select City ---</option>";
    foreach($a as $b)
    {
    ?>
    <option value="<?php echo $b ;?>"><?php echo city($b); ?></option>
    <?php       
    }
}
于 2015-07-28T11:28:39.807 に答える