PHP проблема рекурсивного изменения значений ключей в массиве JSON - PullRequest
0 голосов
/ 17 июня 2011

Вот отрывок из структуры php_decoded 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-- Оператор печати только для отладки. Это можно игнорировать.

Редактировать 2: Вот как эта функция называется:

foreach ($_POST as $postkey => $postvalue)
{
        if ($postvalue != ""){
                print "$postkey => $postvalue\n";
                $idCounter = 1;
                replaceVal($json['NBBList']['$values'][0], $postkey, $postvalue, $idCounter, 0);
        }
}

Опять же, оператор print предназначен для отладки. Дополнительная информация: Имена полей ввода HTML являются динамически назначаемыми номерами в зависимости от их порядка в дереве JSON. Таким образом, приращение переменной idCounter соответствует переходу к следующему полю ввода.
Edit3: добавлено в комментарии к коду.

Ответы [ 2 ]

1 голос
/ 17 июня 2011

Вы можете (и должны) всегда использовать внутреннюю функцию PHP, если она есть.

Если вам не нужен счетчик, вы можете посмотреть на array_replace_recursive . В этом случае ваш код будет выглядеть так:

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

EDIT

После текущих комментариев:

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 голосов
/ 31 октября 2015

Есть еще один способ сделать это.Основная идея состоит в том, чтобы рассматривать JSON как строку, а затем использовать str_replace или preg_replace (str_replace для регулярного выражения).Вот пример:

# 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...