Mike Q
Mike Q

Reputation: 7327

removing duplicate arrays in multidimensional array in PHP

For some reason I am having trouble with this, I have a multi-dimensional array in PHP like the following below (MySQL data). What I want to do is remove any duplicate arrays based on "dealerID".

In the example below, I want to search through the arrays and remove any of duplicate dealerID arrays.

Example, take this:

array(2) {
  [0]=>
  array(3) {
    ["dealerID"]=>
     string(3) "634"
    ["Name"]=>
    string(15) "Dealer Name String"
    ["Other Name"]=>
    string(15) "Dealer Name String"
  }
  [1]=>
  array(3) {
    ["dealerID"]=>
     string(3) "777"
    ["Name"]=>
    string(15) "Dealer Name String"
    ["Other Name"]=>
    string(15) "Dealer Name String"
  }
  [2]=>
  array(2) {
    ["dealerID"]=>
     string(3) "777"
    ["Name"]=>
    string(15) "Dealer Name String"
  }
  [3]=>
  array(3) {
    ["dealerID"]=>
     string(3) "777"
    ["Name"]=>
    string(15) "Dealer Name String"
    ["Other Name"]=>
    string(15) "Dealer Name String"
  }
  [4]=>
  array(2) {
    ["dealerID"]=>
     string(3) "777"
    ["Name"]=>
    string(15) "Dealer Name String"
  }

Desired result, only arrays with unique dealerID:

array(2) {
  [0]=>
  array(3) {
    ["dealerID"]=>
     string(3) "634"
    ["Name"]=>
    string(15) "Dealer Name String"
    ["Other Name"]=>
    string(15) "Dealer Name String"
  }
  [1]=>
  array(3) {
    ["dealerID"]=>
     string(3) "777"
    ["Name"]=>
    string(15) "Dealer Name String"
    ["Other Name"]=>
    string(15) "Dealer Name String"
  }

What I have tried:

$copy = $array; // create copy to delete dups from
$used = array(); // used emails

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

if ( in_array( $array[$i][0], $used ) ) {
    unset($copy[$i]);
}
else {
    $used[] = $array[$i][0];
}

}

and:

foreach($dealerList as $key => $subarray) {

    if ( in_array( $dealerList[$subarray], $used ) ) {
            echo "ALREADY";
     }
     else {
          $used[] = $dealerList[$subarray];
           echo "NOT ALREADY";
     }

    }
    #$dealerList = $copy;

Upvotes: 1

Views: 61

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Short solution using array_values function:

// $arr is your exemplary array from the question
$result = [];
foreach ($arr as $v) {
    $result[$v['dealerID']] = $v;
}
$result = array_values($result);

print_r($result);

The output:

Array
(
    [0] => Array
        (
            [dealerID] => 634
            [Name] => Dealer Name String
            [Other Name] => Dealer Name String
        )

    [1] => Array
        (
            [dealerID] => 777
            [Name] => Dealer Name String
            [Other Name] => Dealer Name String
        )
)

Upvotes: 0

Mickey
Mickey

Reputation: 534

$arr=array(
    array(
        'dealerID'=>344,
        'Name'  => 'sdds',
        'Other Name'
    ),
    array(
        'dealerID'=>400,
        'Name'  => 'sdds',
        'Other Name'
    ),
    array(
        'dealerID'=>400,
        'Name'  => 'sdds',
        'Other Name'
    ),
    array(
        'dealerID'=>347,
        'Name'  => 'sdds',
        'Other Name'
    ),
);


foreach($arr as $key=>$item){
    for($i=$key+1; $i<count($arr);$i++){
        if($item['dealerID']==$arr[$i]['dealerID']){
            unset($arr[$i]);
        }
    }
}
print_r($arr);

Upvotes: 1

Related Questions