Here is an excerpt from the JSON php_decoded structure I'm working with:
array(3) { ["$type"]=> string(51) "NanoWebInterpreter.WebInputData, NanoWebInterpreter" ["NBBList"]=> array(2) { ["$type"]=> string(81) "System.Collections.Generic.List`1[[monoTNP.Common.NBB, monoTNP.Common]], mscorlib" ["$values"]=> array(1) { [0]=> array(6) { ["$type"]=> string(34) "monoTNP.Common.NBB, monoTNP.Common" ["ID"]=> string(16) "id-0065-00000003" ["MPList"]=> array(2) { ["$type"]=> string(80) "System.Collections.Generic.List`1[[monoTNP.Common.MP, monoTNP.Common]], mscorlib" ["$values"]=> array(3) { [0]=> array(9) { ["$type"]=> string(43) "monoTNP.Common.EllipticalMP, monoTNP.Common" ["Eccentricity"]=> float(1) ["ID"]=> string(16) "id-0065-00000006" ["ParticleIndex"]=> int(-1) ["DispersionInteractionStrength"]=> float(0) ["DispersionInteractionRange"]=> float(2.5) ["CharacteristicSize"]=> float(0) ["CenterOfMass"]=> string(7) "<0,0,0>" ["OrientationVector"]=> string(2) "<>" }
I am trying to write this function that recursively traces a JSON object and replaces the target value with the value of $ postvalue, but whenever I try to do this recursively, the value does not change. Here is my code:
function replaceVal(&$json, $postkey, $postvalue, &$idCounter, $level) { $depth = 3;
The interesting part is that if I directly index into the structure as follows:
$key = &$json['NBBList']['$values'][0]['MPList']['$values'][0]['Eccentricity'] $key = "asdf";
The value can be changed. The only thing that seems to be the problem is recursion. This sounds like a very simple problem to fix, but I only programmed a little less than a year, so probably I just don't see anything obvious. >. >
Oh, and the values of postvalue and postkey are derived from the presentation of the HTML form.
- edit-- The print statement is there for debugging. It can be ignored.
Edit 2: This is how the function is called:
foreach ($_POST as $postkey => $postvalue) { if ($postvalue != ""){ print "$postkey => $postvalue\n"; $idCounter = 1; replaceVal($json['NBBList']['$values'][0], $postkey, $postvalue, $idCounter, 0); } }
Again, the print expression is for debugging purposes. Additional Information: The names of the HTML input fields are dynamically assigned numbers according to their order in the JSON tree. Thus, the increment of the idCounter variable corresponds to the transition to the next input field.
Edit3: added to the code comment.