2017-08-02 24 views
0

我有兩個相似的對象: 對象老:獲取類似對象之間的鍵和值的區別在PHP

object(Party)#203 (16) { 
    ["connection":protected]=> 
    object(Connection)#202 (6) { 
    ["token":"Connection":private]=> 
    string(64) "xyzxxyzxyz" 
    ["url":"Connection":private]=> 
    string(33) "https://api.test.com" 
    ["singularAction"]=> 
    string(5) "party" 
    ["pluralAction"]=> 
    string(7) "parties" 
    ["action"]=> 
    string(5) "party" 
    } 
    ["emailAddresses"]=> 
    array(3) { 
    [0]=> 
    array(3) { 
     ["id"]=> 
     int(273530863) 
     ["type"]=> 
     NULL 
     ["address"]=> 
     string(19) "[email protected]" 
    } 
    [1]=> 
    array(3) { 
     ["id"]=> 
     int(274183291) 
     ["type"]=> 
     NULL 
     ["address"]=> 
     string(18) "[email protected]" 
    } 
    [2]=> 
    array(3) { 
     ["id"]=> 
     int(278650133) 
     ["type"]=> 
     NULL 
     ["address"]=> 
     string(26) "[email protected]" 
    } 
    } 
} 

和對象的更新:

object(Party)#201 (16) { 
    ["connection":protected]=> 
    object(Connection)#202 (6) { 
    ["token":"Connection":private]=> 
    string(64) "xyzxxyzxyz" 
    ["url":"Connection":private]=> 
    string(33) "https://api.test.com" 
    ["singularAction"]=> 
    string(5) "party" 
    ["pluralAction"]=> 
    string(7) "parties" 
    ["action"]=> 
    string(5) "party" 
    } 
    ["emailAddresses"]=> 
    array(4) { 
    [0]=> 
    array(3) { 
     ["id"]=> 
     int(273530863) 
     ["type"]=> 
     NULL 
     ["address"]=> 
     string(19) "[email protected]" 
    } 
    [1]=> 
    array(3) { 
     ["id"]=> 
     int(274183291) 
     ["type"]=> 
     NULL 
     ["address"]=> 
     string(18) "[email protected]" 
    } 
    [2]=> 
    array(3) { 
     ["id"]=> 
     int(278650133) 
     ["type"]=> 
     NULL 
     ["address"]=> 
     string(26) "[email protected]" 
    } 
    [3]=> 
    array(1) { 
     ["address"]=> 
     string(27) "[email protected]" 
    } 
    } 
} 

視覺上的差異是最後的電子郵件地址作爲更新對象中的最後一個條目出現。我如何突出顯示PHP的差異並抓住它? 我想檢查兩個對象的每個條目,找出關鍵和值的差異,並用它做一些事情。

我已經成功地寫是這樣的:

//Get the updated object.... 
     $newObject = $this; 
     //Now get the object before update was executed... 
     $oldObj = $this->oldObject; 

     //Remove object memebers from the new object for easy comparison with the old... 
     unset($newObject->oldObject, $newObject->about, $newObject->createdAt, $newObject->updatedAt, $newObject->lastContactedAt, $newObject->pictureURL); 
     //Remove object memebers from the old object.... 
     unset($oldObj->about, $oldObj->createdAt, $oldObj->updatedAt, $oldObj->lastContactedAt, $oldObj->pictureURL); 

     $varsNewObj = get_object_vars($newObject); 
     $varsOldObj = get_object_vars($oldObj); 
     //Remove further unneeded memebers.... 
     unset($varsNewObj['connection']); 
     unset($varsOldObj['connection']); 

     $arrayData = []; 

     foreach ($varsNewObj as $values) { 
      if (is_string($values) && $values != null) { 
       var_dump('New Values: ' . $values); 
      } 
      if (is_array($values)) { 
       foreach ($values as $value) { 
        if (is_string($value) && $value != null) { 
         //var_dump($value); 
        } 
        if (is_array($value)) { 
         foreach ($value as $v) { 
          if ($v != null) { 
           //var_dump($v); 
          } 
         } 
        } 
       } 
      } 
     } 

什麼上面的代碼所做的是讓每一個需要來自新對象比較字符串。同樣的原則可以應用於$ oldObj,但是我對後來如何做比較感到困惑,因爲我腦海中似乎並不奏效。

+0

你可以用'get_object_vars'([手動鏈接](http://php.net/manual/function.get-object-vars.php))開始並比較它們。它是否必須遞歸? – Kaddath

+0

不幸的是。 –

+0

以及我沒有時間爲你編寫它(而不是網站的目的),但我寫了類似的JavaScript遞歸函數(實際上是爲了找到對象結構中的特定鍵),如果你感興趣可以激勵你 – Kaddath

回答

0

注意:這不是一個真正的答案,只是一種方法來解決他的問題,因爲我沒有時間來完全解釋,但仍然想要幫助。你可以投票,如果你喜歡,但沒有必要,這個答案可以刪除,如果問題更新與提問者的嘗試。

所以這是一個遞歸地做類似事情的基本例子。我在這裏直接返回找到的元素,在這種情況下,您應該填充一個數組(如果想要將原始值和修改值的結果分開,則可以填充兩個元素)。你也不應該像我那樣突破,因爲當你不是時,我只希望得到一個結果。

測試if('scope' in e_t){是這裏的遞歸條件,在你的情況下,它將是一個測試值的類型,以知道你是否想更深入(數組,對象?),你沒有精確什麼類型你想要搜索遞歸的元素,如果它們全部都是,你可能不得不在類型上做出例子(不同的方式來迭代屬性)。你也沒有精確的輸出格式,所以你必須這樣做。

的例子:

  function _recursiveSearch(scope, objId, identifier){ 
       var scope_t = scope, e_t, k_t, r_t = false; 
       for(k_t in scope_t){ 
        if(scope_t.hasOwnProperty(k_t)){ 
         e_t = scope_t[k_t]; 
         if(identifier in e_t && e_t[identifier] === objId){ 
          return e_t; 
         } 
         if('scope' in e_t){ 
          r_t = _recursiveSearch(e_t.scope, objId, identifier); 
          if(r_t !== false){ 
           break; 
          } 
         } 
        } 
       } 
       return r_t; 
      } 

編輯PHP:還有我忘了提棘手的部分,要小心,===與PHP對象將比較他們的參考。如果要比較屬性名稱及其值,請使用==。對於方法論來說,確保不要因爲它們不相同而將2個屬性記錄爲不同,因爲如果它們是對象,它們可能是它們的內部屬性之一,事實上並非如此簡單!

+0

感謝您的信息,但我仍然無法弄清楚正確的方法。出於某種原因或其他原因,比較不總是正確執行。 –

+0

@ ao-pack我編輯了您的評論,在某一時刻,您還應該使用您編寫的代碼編輯您的問題,這將對人們更容易幫助 – Kaddath

+0

我更新了一些問題。如果你能看一看,並指向我認爲合適的方式,那就太好了。我知道可能有什麼不對,但現在我沒辦法做得更好,因爲我沒有太多經驗。 –

相關問題