MorganFreeFarm
MorganFreeFarm

Reputation: 3733

Search in associative array, then remove elements that are same by some value?

What i want? From this array, let's call it $forsend :

array(3) {
  [7493474]=>
  array(6) {
    ["id"]=>
    int(1414594)
    ["product_id"]=>
    int(1)
    ["ins_type"]=>
    string(4) "zzzz"
    ["phone"]=>
    string(10) "1111111111"
    ["exam_date"]=>
    string(10) "28.07.2018"
    ["status"]=>
    int(1)
  }
  [7578595]=>
  array(6) {
    ["id"]=>
    int(1414969)
    ["product_id"]=>
    int(1)
    ["ins_type"]=>
    string(4) "zzzz"
    ["phone"]=>
    string(10) "2222222222"
    ["exam_date"]=>
    string(10) "28.07.2018"
    ["status"]=>
    int(1)
  }
  [7583863]=>
  array(6) {
    ["id"]=>
    int(1415124)
    ["product_id"]=>
    int(1)
    ["ins_type"]=>
    string(4) "zzzz"
    ["phone"]=>
    string(10) "1111111111"
    ["exam_date"]=>
    string(10) "28.07.2018"
    ["status"]=>
    int(1)
  }
}

I want to remove these records which are with same phone (1111111111 in that case, but can have multiple records which are the same), without first one and result will be:

    array(2) {
      [7493474]=>
      array(6) {
        ["id"]=>
        int(1414594)
        ["product_id"]=>
        int(1)
        ["ins_type"]=>
        string(4) "zzzz"
        ["phone"]=>
        string(10) "1111111111"
        ["exam_date"]=>
        string(10) "28.07.2018"
        ["status"]=>
        int(1)
      }
      [7578595]=>
      array(6) {
        ["id"]=>
        int(1414594)
        ["product_id"]=>
        int(1)
        ["ins_type"]=>
        string(4) "ГО"
        ["phone"]=>
        string(10) "2222222222"
        ["exam_date"]=>
        string(10) "28.07.2018"
        ["status"]=>
        int(1)
      }
    }

What i tried? Well.. this code:

    foreach($forsend as $x) {
        var_dump($x);
        $foundKey = array_search($x['phone'], array_column($forsend, 'phone'));
        unset($forsend[$foundKey]);
    }

At the moment it doesn't work, it found keys, but not remove it and i don't know why.

Upvotes: 0

Views: 42

Answers (1)

Danielius
Danielius

Reputation: 837

You could try to use this code, it should work if I understood what you need properly.

<?php
$phones = [];
$final = [];
foreach($forsend as $key => $record) {
    if(!isset($phones[$record['phone']]))
        //reversing - now we have an array which is arranged using phone number as a key, 
        //meaning that only one record can exist with the same number
        $phones[$record['phone']] = $key; 
    else
        //if the record is repeated, remove the previous instance, and do not add this one 
        //to the array
        unset($phones[$record['phone']]);
}
foreach($phones as $phone){ 
    //reversing again, getting the format that was used before, from $phone the key of the 
    //top array is returned and using that key the data is taken from
    //$forsend and put to final
    $final[$phone] = $forsend[$phone];
}
print_r($final);

To leave the first element that has the same phone, you should just remove the else clause in the if and it should be enough:

<?php
$phones = [];
$final = [];
foreach($forsend as $key => $record) {
    if(!isset($phones[$record['phone']]))
        $phones[$record['phone']] = $key;
}
foreach($phones as $phone){ 
    $final[$phone] = $forsend[$phone];
}
print_r($final);

Upvotes: 1

Related Questions