I have a multidimensional array in php, which I want to manipulate according to some rules.
Input-Array: (Variable printed as JSON)
[
{
"meta":{
"title": "Adressdata",
"name": "adress"
},
"data":{
"desc":{ "tooltip": "text",
"maxlength": "100"
},
"line1":{ "tooltip": "Recipient",
"maxlength": "40"
}
}
]
Rules:
{
"0>meta>title": "Companyaddress",
"0>data>desc": {
"tooltip": "Another Text",
"maxLength": "150"
}
}
(There are 2 rules, the index explains which field from the input-array has to be changed, and the value contains the data to be inserted instead. Note that the second rule wants to insert an object)
Problem:
I want to change the content of the input-array according to the rules, but I struggle in doing that.
Here's the php-Code I already have:
<?php
$input = json_decode(file_get_contents("inputData.json"),true);
$rules = json_decode(file_get_contents("rules.json"),true);
foreach ($rules as $target => $replacement) {
//rule: "0>meta>title": "newTitle"
$node = $input;
foreach (explode(">",$target) as $index) {
$node = $node[$index];
}
$node = $replacement; //replace doesn't work
}
echo "Result-Array: ";
print_r($input);
?>
Everything works, in except that I can't change the values. Why? Because everytime I set $node-Variable, I create a new Variable. And I only change the content of $node, but not the content of $input.
So I tried to use references - which doesn't work either: When I change the 2 $node-Lines to this code:
$node = &$input;
[...]
$node = &$node[$keys[$i]]; //go to child-node
But this doesn't work, because the second line, where I just want to navigate to a child-node, changes the parent-element (because $node is a reference).
I have no idea if there's a trick to do that, I hope someone can help me
Input-Array.$input = json_decode(file_get_contents("path.json"),true);