2016-01-04 77 views
0

我從一個PHP陣列的輸出如下:PHP - 數組中基於鍵的值和空值刪除重複值

Array ( 
[0] => Array ( 
    [set1] => Array ( 
      [link] => xyz.com/def 
      [time] => 2016-01-03 
    [set2] => Array ( 
      [link] => 
      [time] =>) 
    [set3] => Array ( 
      [link] => 
      [time] =>) 
    ) 
[1] => Array ( 
    [set1] => Array ( 
      [link] => xyz.com/123 
      [time] => 2016-01-03) 
    [set2] => Array ( 
      [link] => 
      [time] =>) 
    [set3] => Array ( 
      [link] => xyz.com/123 
      [time] => 2016-01-03) 
    ) 
[2] => Array ( 
    [set1] => Array ( 
      [link] => xyz.com/abc 
      [time] => 2015-12-03) 
    [set2] => Array (
      [link] => xyz.com/abc 
      [time] => 2016-01-03) 
    [set3] => Array ( 
      [link] => xyz.com/123456 
      [time] => 2016-01-03) 
    )) 

我想消除重複[link]不管[time]的 - 然後我想消除空[link]值,如[0][set2],終於有一個輸出如下:

[link] => xyz.com/def 
[time] => 2016-01-03 

[link] => xyz.com/123 
[time] => 2016-01-03) 

[link] => xyz.com/abc 
[time] => 2015-12-03 

[link] => xyz.com/123456 
[time] => 2016-01-03 

這是我的嘗試:

$categoryUrlArray= array_unique(($categoryUrlArray, SORT_REGULAR); 
    foreach ($categoryUrlArray as $categoryUrlLevel01) { 
     $categoryUrlLevel01= array_unique($categoryUrlLevel01, SORT_REGULAR); 
     foreach ($categoryUrlLevel01 as $categoryUrlLevel02) { 
      echo $categoryUrlLevel02['link']; 
      echo '<br/>'; 
      echo $categoryUrlLevel02['time']; 
      echo '<br/><br/><br/>'; 
     } 
    } 

與上面的代碼的問題是,重複不被淘汰,空值仍顯示出,即有大量換行符<br/> - 這就是爲什麼我知道他們沒有被消除。

+0

是否鏈接有'
'在裏面? – Samir

+0

不,我不使用'
'來使結果輸出時更加用戶友好。 – xltmt

回答

2

怎樣的inner loop裏面只是檢查blanksduplicates?然後將它們全部保存在一個新變量中。這樣

$newArr = array(); 
$links = array(); 
foreach($categoryUrlArray as $urls){ 
    foreach($urls as $url){ 
     if(!empty($url['link']) && !in_array($url['link'],$links)){ 
      $newArr[] = $url; 
      $links[] = $url['link']; 
     } 
    } 
} 
var_dump($newArr); 

將導致以

array(4) { 
    [0]=> 
    array(2) { 
    ["link"]=> 
    string(11) "xyz.com/def" 
    ["time"]=> 
    string(10) "2016-01-03" 
    } 
    [1]=> 
    array(2) { 
    ["link"]=> 
    string(11) "xyz.com/123" 
    ["time"]=> 
    string(10) "2016-01-03" 
    } 
    [2]=> 
    array(2) { 
    ["link"]=> 
    string(11) "xyz.com/abc" 
    ["time"]=> 
    string(10) "2015-12-03" 
    } 
    [3]=> 
    array(2) { 
    ["link"]=> 
    string(14) "xyz.com/123456" 
    ["time"]=> 
    string(10) "2016-01-03" 
    } 
} 
+0

優秀!它像一個魅力。謝謝。 – xltmt

+0

np。很高興我幫助:) – roullie

0

只需添加一個條件,以檢查是否鏈接不爲空!empty($categoryUrlLevel02['link'])

下面是修改後的代碼:

($categoryUrlArray= array_unique(($categoryUrlArray, SORT_REGULAR); 
foreach ($categoryUrlArray as $categoryUrlLevel01) { 
    $categoryUrlLevel01= array_unique($categoryUrlLevel01, SORT_REGULAR); 
    foreach ($categoryUrlLevel01 as $categoryUrlLevel02) { 
     if(!empty($categoryUrlLevel02['link'])) 
     { 
      echo $categoryUrlLevel02['link']; 
      echo '<br/>'; 
      echo $categoryUrlLevel02['time']; 
      echo '<br/><br/><br/>'; 
     } 
    } 
}