2015-08-28 50 views
1

我正在使用爲僱員帶回假期數據的API。數據包含已批准的假期請求以及已取消的假期請求。我現在面臨的問題是,取消了假期的請求顯示在下面的格式重複的條目:忽略來自多維數組的重複條目

stdClass Object 
    (
    [leave_requests] => Array 
    (
     [0] => stdClass Object 
      (
       [employee] => stdClass Object 
        (
         [id] => 30771 
        ) 

       [reviewed_by] => stdClass Object 
        (
         [id] => 22734 
        ) 

       [reason] => 
       [type] => Holiday 
       [deducted] => 1.0 
       [cancelled] => 
       [id] => 626214 
       [start_date] => 2015-08-20 
       [half_start] => 
       [half_start_am_pm] => 
       [end_date] => 2015-08-20 
       [half_end] => 
       [half_end_am_pm] => 
       [action] => request 
       [status] => approved 
       [notes] => 
       [created_at] => 2015-08-06T21:55:12+01:00 
       [updated_at] => 2015-08-07T08:26:07+01:00 
      ) 

     [1] => stdClass Object 
      (
       [employee] => stdClass Object 
        (
         [id] => 30771 
        ) 

       [reviewed_by] => stdClass Object 
        (
         [id] => 22734 
        ) 

       [reason] => 
       [type] => Holiday 
       [deducted] => 1.0 
       [cancelled] => 
       [id] => 632745 
       [start_date] => 2015-08-20 
       [half_start] => 
       [half_start_am_pm] => 
       [end_date] => 2015-08-20 
       [half_end] => 
       [half_end_am_pm] => 
       [action] => cancel 
       [status] => approved 
       [notes] => 
       [created_at] => 2015-08-12T17:50:32+01:00 
       [updated_at] => 2015-08-12T17:53:46+01:00 
      ) 

    ) 
) 

理想情況下,數據將被格式化,因此,在取消財產被利用,我可以過濾掉的是(這有被開發商要求)。我想要做的是刪除具有相同的start_date和end_date的條目。

目前我能夠刪除重複的,通過使用該功能

function super_unique($array) 
    { 
     $newArr = array(); 
     foreach ($array as $val) { 
      $newArr[$val['startDate']] = $val; 
     } 
     $array = array_values($newArr); 

     return $array; 
    } 

夫婦的這個問題,在我仍然留下了其中一個條目,作爲假日請求已取消我不希望他們中的任何一位在數據中。過濾數據以排除具有「取消」動作屬性的所有元素仍然會讓我保留原始請求。

另一個問題是,上述函數僅基於關閉start_date而不是start_date和end_date。

看過關於this SO question的評論,以及關於array_values和array_unique的PHP文檔頁面的評論。

回答

1

我已經取得了一些細微的修改,以你目前的代碼,請參見注釋代碼:

function super_unique($array) 
{ 
    $newArr = array(); 

    foreach ($array as $val) { 
     // Create a key by combining start/end dates 
     $key = $val['startDate'].$val['endDate']; 

     // Have we already seen this start/end date combination? 
     if (array_key_exists($key, $newArr)) { 
      $val = null; // Clear element value, like "remove this later on" 
     } 
     $newArr[$key] = $val; // Add to/update key index (actual value or null) 
    } 

    // Remove all elements with unset (null) values 
    $array = array_filter(array_values($newArr)); 

    return $array; 
} 

$a = [['startDate' => '2015-08-20', // Exists twice - should be removed 
     'endDate' => '2015-08-22'], 
     ['startDate' => '2015-08-20', // Unique start/end combination 
     'endDate' => '2015-08-20'], 
     ['startDate' => '2015-08-21', // Unique start/end combination 
     'endDate' => '2015-08-21'], 
     ['startDate' => '2015-08-20', // Exists twice - should be removed 
     'endDate' => '2015-08-22'], 
     ['startDate' => '2015-08-22', // Unique start/end combination 
     'endDate' => '2015-08-20'], 
]; 

print_r(super_unique($a)); 

輸出:

Array 
(
    [1] => Array 
     (
      [startDate] => 2015-08-20 
      [endDate] => 2015-08-20 
     ) 

    [2] => Array 
     (
      [startDate] => 2015-08-21 
      [endDate] => 2015-08-21 
     ) 

    [3] => Array 
     (
      [startDate] => 2015-08-22 
      [endDate] => 2015-08-20 
     ) 
) 
+0

絕對完美的,謝謝 – terrorfall

+0

不客氣:) – mhall