Remove duplicate items from an array [duplicate]
Asked Answered
H

8

25

I use the line of code below to loop through a table in my database:

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

And if I print the array out:

print_r($items_thread);

I will get this:

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => [email protected]
        )

    [1] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => [email protected]
        )

    [2] => Array
        (
            [RecipientID] => 1
            [RecipientScreenname] => Lau T
            [RecipientFirstname] => TK
            [RecipientEmail] => [email protected]
        )

)

But I want to get rid of the duplicate items in the array, so I use array_unique

print_r(array_unique($items_thread));

I get the weird result below which is not quite I am looking for:

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => [email protected]
        )

)

Ideally, I think it should return this:

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => [email protected]
        )

    [1] => Array
        (
            [RecipientID] => 1
            [RecipientScreenname] => Lau T
            [RecipientFirstname] => TK
            [RecipientEmail] => [email protected]
        )

)

What shall I do to get it right? Have I used the wrong PHP syntax/default function?

Heterosis answered 18/2, 2011 at 0:25 Comment(2)
array_unique() is reducing everything to a single array because it's comparing your inner arrays as strings, all of which evaluate to Array. So every array is considered the same. Additionally, from the manual: "Note that array_unique() is not intended to work on multi dimensional arrays."Teplitz
thank you for your suggestion - SELECT DISTINCT RecipientID. It does not return the result I need, even though I can get the unique value from it... you can have a look on the post i made earlier on here - #5033145 thanks.Heterosis
B
69

The array_unique function will do this for you. You just needed to add the SORT_REGULAR flag:

$items_thread = array_unique($items_thread, SORT_REGULAR);

However, as bren suggests, you should do this in SQL if possible.

Brucebrucellosis answered 18/2, 2011 at 0:29 Comment(0)
L
7

You'd be much better off filtering out the duplicates in the SQL query. add a constraint which fetches a UNIQUE recipientID

Lettielettish answered 18/2, 2011 at 0:28 Comment(4)
SELECT DISTINCT RecipientID ...Teplitz
@Teplitz thanks... Could you explain why this way is better than array_unique() ?Estis
@CJ Ramki: You waste less resources by only selecting the rows that you need and not grabbing everything from the database and throwing away the ones that you don't need.Teplitz
So, if I have array like this, array_unique() will compare the string Arrayand Array and produce the result?Estis
R
4

To remove duplicate values then we can use array_unique() function. The functionality of it is to accept an array and returns another array without duplicate values.

General description of array_unique() is given below:

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 

Example 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));
?>

Output:

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
)
Rella answered 3/7, 2012 at 5:0 Comment(0)
E
1

Try this:

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

Outputs the following:

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => [email protected]
        )

    [2] => Array
        (
            [RecipientID] => 1
            [RecipientScreenname] => Lau T
            [RecipientFirstname] => TK
            [RecipientEmail] => [email protected]
        )
)

But I also think you should implement this in your database. Also, check my other answer and solutions.

Epiphysis answered 18/2, 2011 at 1:0 Comment(4)
thank you for this! what about $items_thread = array_unique($items_thread, SORT_REGULAR); which returns the same result as your code?Heterosis
@lauthiamkok: I've never done that, does it really return the same output? If yes, great!Epiphysis
@lauthiamkok: I've read the manual and that flag seems to deliver the correct results, however bare in mind that it's only available in PHP >= 5.2.9.Epiphysis
thanks. I'm on php 5.3.x and my live will be on 5.3.x too so I think I am safe! thank you very much!Heterosis
K
1

You can use this code and I hope it will help you. It's completely working:

$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); 
Ketchum answered 29/9, 2016 at 13:22 Comment(0)
R
0

You can use the regular php arrays to achieve this.

$newArray = array();
foreach ($origArray as $user)
{
   $newArray[$user['RecipientID']] = $user;
}
Repugn answered 18/2, 2011 at 0:30 Comment(0)
N
0

Please check below code, I hope this is help full for you.

$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;

}

Nominalism answered 10/7, 2015 at 6:46 Comment(0)
E
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       
    }
}
Elsy answered 28/7, 2015 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.