Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving duplicate elements from bidemensional array php
    primarykey
    data
    text
    <p>I have 2 arrays: 1 that holds file names that contain an ID in the name and 2 that contains some data as:</p> <pre><code>Array ( [0] =&gt; Array ( [file] =&gt; 103135_cara.jpg ) [1] =&gt; Array ( [file] =&gt; 103135_corpo.jpg ) [2] =&gt; Array ( [file] =&gt; 103136_cara.jpg ) [3] =&gt; Array ( [file] =&gt; 103136_corpo.jpg ) Array2 ( [0] =&gt; Array ( [id] =&gt; 103137 [nome] =&gt; Eduardo Vieira [sexo] =&gt; 1 [datanascimento] =&gt; 1983-11-15 [morada] =&gt; R: Gothard Kaesemodel 750 ? Torre 1 - Ap 508 [localidade] =&gt; Joinville [cp1] =&gt; [cp2] =&gt; [tlm] =&gt; 479946464 [email] =&gt; eduardo@wetzel.com.br [estadocivil] =&gt; 1 [profissao] =&gt; 7 ) [1] =&gt; Array ( [id] =&gt; 103138 [nome] =&gt; João Nuno Gonçalves [sexo] =&gt; 1 [datanascimento] =&gt; 1984-08-13 [morada] =&gt; Rua Elias Garcia Nº325 6D [localidade] =&gt; Amadora [cp1] =&gt; 2700 [cp2] =&gt; 323 [tlm] =&gt; 964359799 [email] =&gt; joaoridebmx@yahoo.com [estadocivil] =&gt; 1 [profissao] =&gt; 7 ) </code></pre> <p>I have merged the arrays into:</p> <pre><code>Array3 ( [0] =&gt; Array ( [id] =&gt; 103137 [nome] =&gt; Eduardo Vieira [sexo] =&gt; 1 [datanascimento] =&gt; 1983-11-15 [morada] =&gt; R: Gothard Kaesemodel 750 ? Torre 1 - Ap 508 [localidade] =&gt; Joinville [cp1] =&gt; [cp2] =&gt; [tlm] =&gt; 479946464 [email] =&gt; eduardo@wetzel.com.br [estadocivil] =&gt; 1 [profissao] =&gt; 7 [file1] =&gt; 103137_cara.jpg ) [1] =&gt; Array ( [id] =&gt; 103137 [nome] =&gt; Eduardo Vieira [sexo] =&gt; 1 [datanascimento] =&gt; 1983-11-15 [morada] =&gt; R: Gothard Kaesemodel 750 ? Torre 1 - Ap 508 [localidade] =&gt; Joinville [cp1] =&gt; [cp2] =&gt; [tlm] =&gt; 479946464 [email] =&gt; eduardo@wetzel.com.br [estadocivil] =&gt; 1 [profissao] =&gt; 7 [file1] =&gt; 103137_cara.jpg [file2] =&gt; 103137_corpo.jpg ) [2] =&gt; Array ( [id] =&gt; 103138 [nome] =&gt; João Nuno Gonçalves [sexo] =&gt; 1 [datanascimento] =&gt; 1984-08-13 [morada] =&gt; Rua Elias Garcia Nº325 6D [localidade] =&gt; Amadora [cp1] =&gt; 2700 [cp2] =&gt; 323 [tlm] =&gt; 964359799 [email] =&gt; joaoridebmx@yahoo.com [estadocivil] =&gt; 1 [profissao] =&gt; 7 [file1] =&gt; 103138_cara.jpg ) [3] =&gt; Array ( [id] =&gt; 103138 [nome] =&gt; João Nuno Gonçalves [sexo] =&gt; 1 [datanascimento] =&gt; 1984-08-13 [morada] =&gt; Rua Elias Garcia Nº325 6D [localidade] =&gt; Amadora [cp1] =&gt; 2700 [cp2] =&gt; 323 [tlm] =&gt; 964359799 [email] =&gt; joaoridebmx@yahoo.com [estadocivil] =&gt; 1 [profissao] =&gt; 7 [file1] =&gt; 103138_cara.jpg [file2] =&gt; 103138_corpo.jpg ) </code></pre> <p>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'</p> <p>Here is the code I used to merge the arrays:</p> <pre><code>foreach ($ids as $val1) { foreach ($files as $key =&gt; $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]); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload