2012-08-06 59 views
0

重复元素我有2个数组:1,其保存包含在名称中包含一些数据作为ID和2的文件名:卸下从bidemensional阵列PHP

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] => [email protected] 
      [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] => [email protected] 
      [estadocivil] => 1 
      [profissao] => 7 
     ) 

我已合并数组为:

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] => [email protected] 
      [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] => [email protected] 
      [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] => [email protected] 
      [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] => [email protected] 
      [estadocivil] => 1 
      [profissao] => 7 
      [file1] => 103138_cara.jpg 
      [file2] => 103138_corpo.jpg 
     ) 

我的问题是:我怎么能删除仅包含键“文件1”中保持有两个键“文件1”和“文件2”

这里的那些数组元素是我使用的代码对我来说RGE数组:

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]); 
    } 
    } 
} 
+0

你是如何合并的阵列?我真的没有看到重复条目出现在哪里或数据如何相关。我的意思是你*可以*只是'foreach($ array为$ key => $ item){if(!isset($ item ['file1'],$ item ['file2'])){unset($ array [$ key]); }} - 但我认为一个更好的解决方案是阻止首先出现的诱惑。 – DaveRandom 2012-08-06 21:20:27

+0

对于array2中的每个条目(其中包含作为array1中文件名称一部分的ID),array1中可以有1或2个条目。每个array1元素几乎总是有2个文件。 – mjpramos 2012-08-07 12:03:17

+0

我试过这个合并。我觉得这里是我的问题: ($ IDS是阵列1和$文件是数组2) – mjpramos 2012-08-07 12:06:31

回答

0
foreach ($array as $nr => $values){ 
    if (isset($values['file1']) && !isset($values['file2']){ 
     unset($array[$nr]); 
    } 
} 
0

尝试:

function filter($element) { 
    $result = isset($element['file1'], $element['file2']); 

    return $result; 
} 

$out = array_filter($arr3, 'filter'); 
var_dump($out); 

其中$ ARR3是你的第三个阵列

array_filter也接受的方法为过滤功能:EX

array_filter($arr3, array('class_name or class instance', 'method_name')); 

//编辑
我用DaveRandom建议有关isset

+0

'isset()'接受多个参数,'$ result'变量没有意义。只是'返回isset($ element ['file1'],$ element ['file2']);' – DaveRandom 2012-08-06 21:23:34

+0

我忘了isset - 谢谢。在我看来像returnset($ a,$ v,$ c)这样的构造很难用调试器来检查 - 临时变量很便宜。 – mrok 2012-08-06 21:28:06

0

最简单的方式,可能运行一个循环,并检查是否钥匙“文件1”存在,关键“文件2”不是:

foreach ($array as $key => $val) { 
    if (isset($val['file1']) && !isset($val['file2'])) { 
     unset($array[$key]); 
    } 
}  
0

因为你的ID的原始数组中出现是唯一的(因为它们是ID和全部),所以将它们用于键是有意义的。这意味着您可以轻松识别每个文件所属的元素。

这是很容易重新建立索引以ID作为密钥的阵列:

$data = array(); 
foreach ($ids as $val) { 
    $data[$val['id']] = $val; 
} 

然后以文件与相应项目相关联,所有你需要做的是:

foreach ($files as $file) { 

    // First get the ID of the associated item 
    $parts = explode('_', $file); 
    $id = $parts[0]; 

    // Now add this file to the correct item 
    // Find the next spare file number for this ID 
    for ($i = 1; isset($data[$id]['file'.$i]); $i++) { 
    continue; 
    } 
    // ...and add the file 
    $data[$id]['file'.$i] = $file; 

} 

这将避免首先产生重复的项目。它也避免了嵌套的foreach - 一般来说,如果你发现自己这样做,你做错了什么。这不是一条硬性规则,但是如果你正在做的是计算多个数据集的可能排列,那么它通常只是有用/正确的方法,而这不是你在这里做的。

现在,我必须说我不会做你所做的事情,并创建file1file2,我会创建一个子阵列files来代替。这将使得稍后循环文件变得更容易,并且它也将避免该检查以找到文件的下一个可用编号。

所以,我会作出这样的第二环是这样的:

foreach ($files as $file) { 

    // First get the ID of the associated item 
    $parts = explode('_', $file); 
    $id = $parts[0]; 

    // Now add this file to the correct item 
    if (!isset($data[$id]['files'])) { 
    $data[$id]['files'] = array(); 
    } 
    $data[$id]['files'][] = $file; 

}