Jump to content

Duplicates in array


BrettCarr

Recommended Posts

Hi guys im trying to get rid of records with duplicate phone numbers here's my array

 

Array (
    [0] => Array
        (
            [0] => 11-1-89677-362123164
            [1] => 11
            [2] => testadmin
            [3] => 61415676836
            [4] => 2011-11-16
            [5] => 1
            [6] => 0
            [7] => 0
            [8] => 
            [9] => 0
            [10] => 0
            [11] => 0
        )

    [1] => Array
        (
            [0] => 11-1-89688-362186603
            [1] => 11
            [2] => testadmin
            [3] => 61415556677
            [4] => 2011-08-12
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 
            [9] => 0
            [10] => 0
            [11] => 0
        )

    [2] => Array
        (
            [0] => 11-1-89689-362186773
            [1] => 11
            [2] => testadmin
            [3] => 61415676836
            [4] => 2011-08-12
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 
            [9] => 0
            [10] => 0
            [11] => 0
        )

    [3] => Array
        (
            [0] => 11-1-89690-362186926
            [1] => 11
            [2] => testadmin
            [3] => 61415223344
            [4] => 2011-08-12
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 
            [9] => 0
            [10] => 0
            [11] => 0
        )
)

I can't quit figure this out any sujestions would be gold,,, this is my attempt but its not working

 

$final = array();
foreach ($phoneNumberArray as $array[3]) {
    if(!in_array($array, $final)){
        $final[] = $array;
    }
}
print_r($final);

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/246153-duplicates-in-array/
Share on other sites

use the array_unique() function

That won't work on values in a sub-array of a mufti-dimensional array.

 

$phones = array();
foreach ($phoneNumberArray as $index => $record)
{
    if(!in_array($record[3], $phones))
    {
        //First record with this phone number. add phone to the list and leave the record
        $phones[] = $record[3];
    }
    else
    {
        //This phone was in a previous record, remove the record
        unset($phoneNumberArray[$index]);
    }
}
print_r($phoneNumberArray);

Link to comment
https://forums.phpfreaks.com/topic/246153-duplicates-in-array/#findComment-1264160
Share on other sites

foreach ($array AS &$a) {
    $a=array_unique($a);
}

 

you can write that as a function and use an if (is_array($a)) to use it for arrays with more than 2 dimensions

 

What? That won't work either. That would ensure that there are no duplicates values within any sub-array. The problem as displayed by the OP is that he wants to ensure there are no duplicates (of a particular value) between sub-arrays.

 

array_unique() only works on values within a single dimension. It would work if those sub-arrays are exactly the same, but the OP only wants to exclude those where one value in the sub-array is a duplicate.

Link to comment
https://forums.phpfreaks.com/topic/246153-duplicates-in-array/#findComment-1264369
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.