0

I have an array looks like this,

Array
(
  [0] => Array
      (
          [id] => 224983
          [name] => James
          [weight] => 0
          [bank] => Bank A
          [transaction] => 1
          [total] => 7682000000
          [reference] => Edward
          [type] => BRANCH
          [reference_id] => 222818
          [account_number] => 1220007355285
      )
  [1] => Array
      (
          [id] => 224984
          [name] => James
          [weight] => 0
          [bank] => Bank A
          [transaction] => 1
          [total] => 7682000000
          [reference] => Edward
          [type] => BRANCH
          [reference_id] => 222819
          [account_number] => 1220007355285
      )
  [3] => Array
      (
          [id] => 224985
          [name] => Maria
          [weight] => 0
          [bank] => Bank B
          [transaction] => 1
          [total] => 1500000000
          [reference] => Andrey
          [type] => BRANCH
          [reference_id] => 247620
          [account_number] => 1220000412901
      )              
)

When the account_number, reference, and name is same I want to remove the other one, and keep the last based on id...

Please someone help me to find out, I've been stuck here, so the output would be remove array[0] with ['id] => 224983, and the rest array would be the result

ENCORED
  • 13
  • 8

3 Answers3

0

Try the following function, This will help to remove duplicates arrays.

function array_unique_multidimensional($input)
{
    $serialized = array_map('serialize', $input);
    $unique = array_unique($serialized);
    return array_intersect_key($input, $unique);
}
LucidKit
  • 28
  • 5
0

If you specifically just want to compare only three field of array than you can try below way. Which checks of duplicate and unset previous entries. Here $data is array input

foreach ($data as $key => $row) {
    $id   = $row['id'];
    $name = $row['name'];
    $reference = $row['reference'];

    $flag = 0;
    for($i = $key + 1; $i < count($data); $i++)
    {
        if(($data[$i]['id'] == $id) && ($data[$i]['name'] == $name) && ($data[$i]['reference'] == $reference))
        {
            if($key != $i)
                unset($data[$key]);
        }
    }
}

Result would be

Array
(
    [1] => Array
        (
            [id] => 224983
            [name] => James
            [weight] => 0
            [bank] => Bank A
            [transaction] => 1
            [total] => 7682000000
            [reference] => Edward
            [type] => BRANCH
            [reference_id] => 222818
            [account_number] => 1220007355285
        )

    [2] => Array
        (
            [id] => 224985
            [name] => Maria
            [weight] => 0
            [bank] => Bank B
            [transaction] => 1
            [total] => 1500000000
            [reference] => Andrey
            [type] => BRANCH
            [reference_id] => 247620
            [account_number] => 1220000412901
        )

)
Nirali
  • 1,776
  • 2
  • 12
  • 23
0

It seems inelegant, but I think you would need to loop through your previous array elements to check IDs. You could create a function something like this:

function id_exists_prior(&$my_array, $array_index) {
    for($i = 0; $i < $array_index; $i++) {
        if($my_array[$i]['id'] === $my_array[$array_index]['id']) return false;
    }
    return true;
}

Then you can call it on any array element you wish, or loop through the whole array to check for duplicates. An example of the latter:

$your_array = [/*Your array data*/];
for($key = 0; $key < count($your_array); $key++) { 
    if(id_exists_prior($your_array, $key)) //Do your thing
}

The reason for passing the array key into the function is so that the function ONLY checks for prior duplicates.

Hope this helps!

Jacob Runge
  • 392
  • 3
  • 13