2011-06-16 71 views
0

下面的键值从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:在注释中添加了代码。

+0

我通过你的描述看到你想用新值替换一个键,但是从给出的代码中,它确实有更多的逻辑。你可以展示一个实例吗? – Lumbendil 2011-06-16 20:50:01

+0

是的,对不起。我试图尽可能保持简单。我在第二次编辑中添加了呼叫。 – Richard 2011-06-16 21:06:20

+0

你能解释一下这个功能究竟是做什么的吗?我能读到的是以下内容:如果级别低于阈值,它只是递归。达到级别时:对于每个元素,它检查是否存在'$ idCounter == $ postvalue'。在这种情况下,它用'$ value'替换该键。在另一种情况下,它只会增加'$ idCounter'。 – Lumbendil 2011-06-16 21:34:12

回答

1

你可以(也应该)总是使用PHP的内部函数,以防万一。

如果你不需要柜台,你可以看看array_replace_recursive。在这种情况下,你的代码应该是这样的:

function replaceVal(&$array, $key, $value) { 
    $array = array_replace_recursive($array, array($key => $value)); 
} 

编辑

目前的意见后:

function replaceVal(&$json, $postkey, $postvalue, &$idCounter, $level) 
{ 
    $depth = 3; 

    if ($level >= $depth){ 
      foreach($json as $key => $value) { 
        if($idCounter == $postkey) { 
          $json[$key] = $postvalue; #Not working properly 
          return; 
        } 
        $idCounter++; 
      } 
    } 

    if ($level < $depth){ 
      foreach($json as $key => $value){ 
        if ($idCounter < $postkey) 
          replaceVal($json[$key], $postkey, $postvalue, $idCounter, $level+1); 
        else 
          return; 
      } 
    } 
} 

的问题是,在递归,你在哪里使用$value,这是数组元素的拷贝。然后,这是编辑,但更改不传播到$json

+0

AH美丽!非常感谢 :) – Richard 2011-06-17 02:08:19

0

还有另一种方法可以做到这一点。主要想法是将JSON视为一个字符串,然后使用str_replacepreg_replace(str_replace for regexp)。有一个示例:

# Creating a mapping array ("old_key_name" => "new_key_name"). 
# There I'm reading a mapping from the json file $mapping_json_file. 
# But you can set your mapping array directly instead, like $mapping_array = array("old_key_name" => "new_key_name",...). 
$mapping_array = json_decode(file_get_contents($mapping_json_file),true); 

# Replace string 
$new_json = str_replace(array_keys($mapping_array ), array_values($mapping_array), $old_json); 

注意:最好使用完全匹配来替换字符串。有一种方法可以做到。

# For each key name, replace each $old_key_name by "/\b".$old_key_name."\b/u". 
# It's needed for recognizing breakers. 
$tmp_arr = array_map(function($k){ return '/\b'.$k.'\b/u'; }, array_keys($mapping_array)); 

# Now we use "/\b".$old_key_name."\b/u" instead $old_key_name. 
$new_json = preg_replace($tmp_arr, array_values($mapping_array), $old_json);