2017-10-15 76 views
0

我有這個PHP陣列放toghether:PHP數組中刪除重複的並且在另一個陣列

Array 
(
    [0] => Array 
     (
      [0] => 2505 
      [1] => Lima 
      [2] => Daniels 
      [3] => 0996995904 
      [4] => 
      [5] => 755971 
      [6] => 1454284800 
      [7] => Cat. A (Moto) 
      [8] => 0 
      [9] => 1 
      [10] => 1 
     ) 

    [1] => Array 
     (
      [0] => 2505 
      [1] => Lima 
      [2] => Daniels 
      [3] => 0996995904 
      [4] => 
      [5] => 755971 
      [6] => 1454284800 
      [7] => Cat. A (Moto) 
      [8] => 1 
      [9] => 0 
      [10] => 0 
     ) 

    [2] => Array 
     (
      [0] => 2505 
      [1] => Lima 
      [2] => Daniels 
      [3] => 0996995904 
      [4] => 
      [5] => 755971 
      [6] => 1454284800 
      [7] => Cat. A (Moto) 
      [8] => 0 
      [9] => 0 
      [10] => 0 
     ) 

    [3] => Array 
     (
      [0] => 2525 
      [1] => Lomarca 
      [2] => Miro 
      [3] => 0099778877 
      [4] => 
      [5] => 768131 
      [6] => 1454976000 
      [7] => Cat. A (Moto) 
      [8] => 1 
      [9] => 0 
      [10] => 0 
     ) 

    [4] => Array 
     (
      [0] => 2525 
      [1] => Lomarca 
      [2] => Miro 
      [3] => 0099778877 
      [4] => 
      [5] => 768131 
      [6] => 1454976000 
      [7] => Cat. A (Moto) 
      [8] => 0 
      [9] => 1 
      [10] => 0 
     ) 

) 

我想我新的數組: 1)如果元素[0]存在我想控制元件陣列中的8 ,9和10,如果其中一個是1,我希望在最終數組中有1,但我不希望有相同的數組2次(索引[0]是鍵)。

我的陣列天線的最終shound是:

Array 
(
    [0] => Array 
     (
      [0] => 2505 
      [1] => Lima 
      [2] => Daniels 
      [3] => 0996995904 
      [4] => 
      [5] => 755971 
      [6] => 1454284800 
      [7] => Cat. A (Moto) 
      [8] => 1 
      [9] => 1 
      [10] => 1 
     ) 

    [3] => Array 
     (
      [0] => 2525 
      [1] => Lomarca 
      [2] => Miro 
      [3] => 0099778877 
      [4] => 
      [5] => 768131 
      [6] => 1454976000 
      [7] => Cat. A (Moto) 
      [8] => 1 
      [9] => 1 
      [10] => 0 
     ) 
) 

我只有一個時間索引2505和2505和索引8和9和10是1的2505和索引8和9是1的2525 。

+0

爲什麼2525項在輸出中有[10] => 0? – RomanPerekhrest

+0

我想是因爲#3和#4都有[10] => 0,所以'OR'將產生0. – Forbs

+0

海報,什麼是主鍵?元素[0]還是別的? – Forbs

回答

0

這樣做的工作:(假設你的第一個數組是$arrays和你期望的結果是$result

$result = array(); 
foreach($arrays as $array) 
{ 
     if(!isset($result[$array[0]])) 
     { 
      $result[$array[0]] = $array; 
     } 
     else 
     { 
      $result[$array[0]][8] = $array[8] == 1 ? 1 : $result[$array[0]][8]; 
      $result[$array[0]][9] = $array[9] == 1 ? 1 : $result[$array[0]][9]; 
      $result[$array[0]][10] = $array[10] == 1 ? 1 : $result[$array[0]][10]; 
     } 
} 

,但無論你來自例如讓您的數據。一個數據庫,我會使用element[0]作爲關鍵,所以您不會在$arrays中獲得多個條目。另一個選擇是創建一個保存您的數據的類...