2016-11-22 68 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){