2016-11-22 66 views
0

在嵌套數組中,我想用y改變鍵x。例如:如何在PHP中更新嵌套數組中的鍵?

clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount 

應該變成:

clonedAssetSource - 2000:clonedAssetSource - 2000._jvxMatchCount. 

我寫了一個遞歸解決方案,但它不工作。這裏是我解碼成一個嵌套的數組的JSON:

{ 
    "defaultAssetGroup":"def", 
    "data":[ 
     { 
      "id":"R-136-11821-1479722207355", 
      "ruleName":"Rule 1", 
      "reportConfig":{ 
       "includeAssetSources":[ 
        "773" 
       ] 
      }, 
      "creative_groups":[ 
       "base1" 
      ], 
      "conditions":[ 
       { 
        "id":"R-136-11821-1479722207355", 
        "operator":"and", 
        "selectors":[ 
         { 
          "id":"TRI-R-136-11821-1479722220855", 
          "comparator":"equals", 
          "device:device.model":"iPhone" 
         }, 
         { 
          "id":"TRI-R-136-11821-1479741956751", 
          "comparator":"equals", 
          "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"20" 
         }, 
         { 
          "id":"TRI-R-136-11821-1479742095900", 
          "comparator":"greater-than", 
          "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"20" 
         }, 
         { 
          "id":"TRI-R-136-11821-1479742096434", 
          "comparator":"less-than", 
          "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"20" 
         } 
        ], 
        "conditions":[ 
         { 
          "id":"R-136-11821-1479744209533", 
          "operator":"and", 
          "selectors":[ 
           { 
            "id":"TRI-R-136-11821-1479744228986", 
            "comparator":"equals", 
            "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"10" 
           }, 
           { 
            "id":"TRI-R-136-11821-1479744230911", 
            "comparator":"greater-than", 
            "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"10" 
           }, 
           { 
            "id":"TRI-R-136-11821-1479744232111", 
            "comparator":"less-than", 
            "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"10" 
           } 
          ], 
          "conditions":[ 
           { 
            "id":"R-136-11821-1479744233099", 
            "operator":"and", 
            "selectors":[ 
             { 
              "id":"TRI-R-136-11821-1479744234649", 
              "comparator":"equals", 
              "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"12" 
             }, 
             { 
              "id":"TRI-R-136-11821-1479744235059", 
              "comparator":"greater-than", 
              "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"12" 
             }, 
             { 
              "id":"TRI-R-136-11821-1479744237266", 
              "comparator":"less-than", 
              "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"12" 
             } 
            ], 
            "conditions":[ 

            ] 
           } 
          ] 
         } 
        ] 
       } 
      ] 
     }, 
     { 
      "id":"R-136-11821-1479744343408", 
      "ruleName":"Rule 2", 
      "reportConfig":{ 
       "includeAssetSources":[ 
        "773" 
       ] 
      }, 
      "creative_groups":[ 

      ], 
      "conditions":[ 
       { 
        "id":"R-136-11821-1479744343408", 
        "operator":"and", 
        "selectors":[ 
         { 
          "id":"TRI-R-136-11821-1479744356803", 
          "comparator":"equals", 
          "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"8" 
         }, 
         { 
          "id":"TRI-R-136-11821-1479744357331", 
          "comparator":"greater-than", 
          "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"8" 
         }, 
         { 
          "id":"TRI-R-136-11821-1479744360713", 
          "comparator":"less-than", 
          "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"8" 
         } 
        ], 
        "conditions":[ 
         { 
          "id":"R-136-11821-1479744362635", 
          "operator":"and", 
          "selectors":[ 
           { 
            "id":"TRI-R-136-11821-1479744363763", 
            "comparator":"equals", 
            "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"9" 
           }, 
           { 
            "id":"TRI-R-136-11821-1479744363991", 
            "comparator":"equals", 
            "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"9" 
           }, 
           { 
            "id":"TRI-R-136-11821-1479744365361", 
            "comparator":"equals", 
            "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"9" 
           } 
          ], 
          "conditions":[ 
           { 
            "id":"R-136-11821-1479744366147", 
            "operator":"and", 
            "selectors":[ 
             { 
              "id":"TRI-R-136-11821-1479744367734", 
              "comparator":"equals", 
              "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"3" 
             }, 
             { 
              "id":"TRI-R-136-11821-1479744367950", 
              "comparator":"greater-than", 
              "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"3" 
             }, 
             { 
              "id":"TRI-R-136-11821-1479744368203", 
              "comparator":"less-than", 
              "clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount":"3" 
             } 
            ], 
            "conditions":[ 

            ] 
           } 
          ] 
         } 
        ] 
       } 
      ] 
     } 
    ], 
    "assetSources":[ 
     { 
      "dataSignal":"tagVarService", 
      "property":"DataSignal1", 
      "assetDatabase":"773", 
      "addToCreativeId":true 
     } 
    ], 
    "reportingKeyFormat":"ruleDefined", 
    "virtualAssets":[ 
     { 
      "assetId":"customAsset.asset103", 
      "assetDatabase":"773", 
      "keyColumn":"Image", 
      "assetProperty":"icon" 
     } 
    ] 
} 

這是我的代碼片段:

function updateConditions(&$conditions, $oldDataServiceName, $newDataServiceName) { 
    foreach($conditions as $condIndex => $condition){ 
     foreach($condition['selectors'] as $selIndex => $sel) 
      foreach($sel as $ruleIndex => $condValue){ 
       if($ruleIndex != 'id' && $ruleIndex !='comparator') { 
        $compareKey = $oldDataServiceName.":".$oldDataServiceName."._jvxMatchCount"; 
        if($ruleIndex == $compareKey) { 
         unset($conditions[$condIndex]['selectors'][$selIndex][$ruleIndex]); 
         $newKey = $newDataServiceName.":".$newDataServiceName."._jvxMatchCount"; 
         $conditions[$condIndex]['selectors'][$selIndex][$newKey] = $condValue; 
        } 
       } 
      } 
     } 
     if(isset($condition['conditions'])){ 
      updateConditions($condition['conditions'], $oldDataServiceName, $newDataServiceName); 
     } 
    } 
    return true; 
} 

,我叫上述功能如下傳遞數組$ rulePackageArr

if(!empty($newDataServiceNames)) { 
    foreach ($newDataServiceNames as $oldDataServiceName => $newDataServiceName) { 
     if(!empty($rulePackageArr['data'])){ 
      foreach($rulePackageArr['data'] as $key => $data){ 
       updateConditions($rulePackageArr['data'][$key]['conditions'], $oldDataServiceName, $newDataServiceName); 
      } 
     } 
    } 
} 
+0

爲什麼不使用正則表達式:您可以通過使$condition變量的引用,與&詞綴解決這個問題? – Eric

+0

請注意,您在第二個'foreach'中缺少左括號。 – trincot

+0

注意:我刪除了'json'標籤。數組從JSON解碼出來的事實在這裏並不重要。 – trincot

回答

1

主要問題是您的遞歸調用不會傳遞對原始數組的引用,而是副本。

foreach($conditions as $condIndex => &$condition){