2012-08-06 51 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; 

}