Rakesh B.K
Rakesh B.K

Reputation: 63

How to update a key in nested array in PHP?

In a nested array I want to change the key x with y. For example:

clonedAssetSource - 1479722826:clonedAssetSource - 1479722826._jvxMatchCount

should become:

clonedAssetSource - 2000:clonedAssetSource - 2000._jvxMatchCount.

I wrote a recursive solution, but it's not working. Here is the JSON that I decoded into a nested array:

{  
    "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"
        }
    ]
}

This is my code snippet:

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;
}

and I called the above function as follows passing it the array $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);
            }
        }
    }
}

Upvotes: 0

Views: 186

Answers (1)

trincot
trincot

Reputation: 351369

The main issue is that your recursive call does not pass a reference to the original array, but a copy. You can solve this by making the $condition variable a reference, with the & affix:

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

Upvotes: 1

Related Questions