下面的键值从php_decoded JSON结构的exerpt,我有工作:PHP问题更改递归JSON阵列
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) "<>"
}
我一直在试图写这个函数,递归地追溯了JSON对象,并替换目标值为$ postvalue,但每当我尝试递归执行此操作时,值都不会更改。这是我到目前为止的代码:
function replaceVal(&$json, $postkey, $postvalue, &$idCounter, $level)
{
$depth = 3;
#Base Case
#At the right depth level, check to see if the idCounter is equal to the
#postkey value (the HTML input field name). If it is, take the
#corresponding key and assign the postvalue to it (the input from form).
#Then return. Otherwise, incrememnt the idCounter and return.
if ($level >= $depth){
foreach($json as $key => $value){
if($idCounter == $postkey){
print "$key => $value\n";
$json[$key] = $postvalue; #Not working properly
return;
}
$idCounter++;
}
}
#Descend down into the level of the tree specified by $depth.
#This level should correspond do the level at which the HTML input
#fields lie
#$idCounter will only be greater than $postkey if the value has
#been changed by the previous if statement. In that case, the job is done
#and the function will terminate.
if ($level < $depth){
foreach($json as $key => $value){
if ($idCounter < $postkey)
replaceVal($value, $postkey, $postvalue, $idCounter, $level+1);
else
return;
}
}
}
有趣的是,如果我直接索引到结构如下所示:
$key = &$json['NBBList']['$values'][0]['MPList']['$values'][0]['Eccentricity']
$key = "asdf";
值是可以改变的。似乎是唯一的问题是递归。这听起来像是一个很容易解决的问题,但我只是编程了不到一年的时间,所以我可能只是缺少一些明显的东西。 >。>
哦,postvalue和postkey值来自HTML表单提交。
--edit-- print语句就在那里进行调试。它可以被忽略。
编辑2: 下面是函数是如何调用:
foreach ($_POST as $postkey => $postvalue)
{
if ($postvalue != ""){
print "$postkey => $postvalue\n";
$idCounter = 1;
replaceVal($json['NBBList']['$values'][0], $postkey, $postvalue, $idCounter, 0);
}
}
再次,打印语句是调试目的。 附加信息:HTML输入字段的名称是根据其在JSON树中的顺序动态分配的数字。因此,递增变量idCounter对应于继续到下一个输入字段。
Edit3:在注释中添加了代码。
我通过你的描述看到你想用新值替换一个键,但是从给出的代码中,它确实有更多的逻辑。你可以展示一个实例吗? – Lumbendil 2011-06-16 20:50:01
是的,对不起。我试图尽可能保持简单。我在第二次编辑中添加了呼叫。 – Richard 2011-06-16 21:06:20
你能解释一下这个功能究竟是做什么的吗?我能读到的是以下内容:如果级别低于阈值,它只是递归。达到级别时:对于每个元素,它检查是否存在'$ idCounter == $ postvalue'。在这种情况下,它用'$ value'替换该键。在另一种情况下,它只会增加'$ idCounter'。 – Lumbendil 2011-06-16 21:34:12