Removing duplicate elements from bidemensional array php

I have 2 arrays: 1 that holds file names that contain an ID in the name and 2 that contains some data as:

Array
(
[0] => Array
(
[file] => 103135_cara.jpg
)

[1] => Array
    (
        [file] => 103135_corpo.jpg
    )

[2] => Array
    (
        [file] => 103136_cara.jpg
    )

[3] => Array
    (
        [file] => 103136_corpo.jpg
    )

Array2
(
[0] => Array
(
[id] => 103137
[nome] => Eduardo Vieira
[sexo] => 1
[datanascimento] => 1983-11-15
[morada] => R: Gothard Kaesemodel 750 ? Torre 1 - Ap 508
[localidade] => Joinville
[cp1] =>
[cp2] =>
[tlm] => 479946464
[email] => eduardo@wetzel.com.br
[estadocivil] => 1
[profissao] => 7
)

[1] => Array
    (
        [id] => 103138
        [nome] => João Nuno Gonçalves
        [sexo] => 1
        [datanascimento] => 1984-08-13
        [morada] => Rua Elias Garcia Nº325 6D
        [localidade] => Amadora
        [cp1] => 2700
        [cp2] => 323
        [tlm] => 964359799
        [email] => joaoridebmx@yahoo.com
        [estadocivil] => 1
        [profissao] => 7
    )

I have merged the arrays into:

Array3
(
[0] => Array
(
[id] => 103137
[nome] => Eduardo Vieira
[sexo] => 1
[datanascimento] => 1983-11-15
[morada] => R: Gothard Kaesemodel 750 ? Torre 1 - Ap 508
[localidade] => Joinville
[cp1] =>
[cp2] =>
[tlm] => 479946464
[email] => eduardo@wetzel.com.br
[estadocivil] => 1
[profissao] => 7
[file1] => 103137_cara.jpg
)

[1] => Array
    (
        [id] => 103137
        [nome] => Eduardo Vieira
        [sexo] => 1
        [datanascimento] => 1983-11-15
        [morada] => R: Gothard Kaesemodel 750 ? Torre 1 - Ap 508
        [localidade] => Joinville
        [cp1] =>
        [cp2] =>
        [tlm] => 479946464
        [email] => eduardo@wetzel.com.br
        [estadocivil] => 1
        [profissao] => 7
        [file1] => 103137_cara.jpg
        [file2] => 103137_corpo.jpg
    )

[2] => Array
    (
        [id] => 103138
        [nome] => João Nuno Gonçalves
        [sexo] => 1
        [datanascimento] => 1984-08-13
        [morada] => Rua Elias Garcia Nº325 6D
        [localidade] => Amadora
        [cp1] => 2700
        [cp2] => 323
        [tlm] => 964359799
        [email] => joaoridebmx@yahoo.com
        [estadocivil] => 1
        [profissao] => 7
        [file1] => 103138_cara.jpg
    )

[3] => Array
    (
        [id] => 103138
        [nome] => João Nuno Gonçalves
        [sexo] => 1
        [datanascimento] => 1984-08-13
        [morada] => Rua Elias Garcia Nº325 6D
        [localidade] => Amadora
        [cp1] => 2700
        [cp2] => 323
        [tlm] => 964359799
        [email] => joaoridebmx@yahoo.com
        [estadocivil] => 1
        [profissao] => 7
        [file1] => 103138_cara.jpg
        [file2] => 103138_corpo.jpg
    )

My question is: how can I remove the the array elements that contain only the key ‘file1’ keeping the ones that have both keys ‘file1’ and ‘file2’

Here is the code I used to merge the arrays (probably where the problem originates):


foreach ($ids as $val1) {
  foreach ($files as $key => $val2) {
    $cara = strpos($val2['file'], $val1['id'].'_cara');
    if ($cara !== false) {
      $val1['file1'] = $val2['file'];
      $data[] = $val1;
      unset($files[$key]);
    }
    $corpo = strpos($val2['file'], $val1['id'].'_corpo');
    if ($corpo !== false) {
      $val1['file2'] = $val2['file'];
      $data[] = $val1;
      unset($files[$key]);
    }
  }
}

Thanks

Well you’re right to think that the issue lies in your merge script.
Lets see if I can understand this.

  1. The ID is the thing before the _ in the file name of your original array.
  2. You want 1 entry per ID; a combined set of filenames.

So my impulse is to take an intermediate step. Transform your original array into an ID-keyed array.


$tempid = array();
foreach($ids AS $id) {
 $splode = explode('_',$id);
 $tempid[$splode[0]][] = $id;
}

Now you’ll have something like this:


Array
(
[103135] => Array
(
[0] => 103135_cara.jpg
[1] => 103135_corpo.jpg
)
[103136] => Array
(
[0] => 103136_cara.jpg
[1] => 103136_corpo.jpg
}

Which should get you closer to what you want.