1

我在 php 中有一个多维数组,我想根据一些规则对其进行操作。

输入数组:( 以 JSON 形式打印的变量)

[
    {
        "meta":{
                "title": "Adressdata",
                "name": "adress"
        },    
        "data":{
            "desc":{    "tooltip":  "text",
                        "maxlength":    "100"
                },
            "line1":{   "tooltip":  "Recipient",
                        "maxlength":    "40"
                }
    }
]

规则:

{
    "0>meta>title": "Companyaddress",
    "0>data>desc": {
        "tooltip": "Another Text",
        "maxLength": "150"
    }
}

(有两条规则,索引说明输入数组中的哪个字段必须更改,值包含要插入的数据。注意第二条规则要插入一个对象)

问题:

我想根据规则更改输入数组的内容,但我很难做到这一点。

这是我已经拥有的 php 代码:

<?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);
?>

一切正常,除了我无法更改值。为什么?因为每次我设置 $node-Variable 时,我都会创建一个新变量。而且我只修改了$node的内容,没有修改$input的内容。

所以我尝试使用引用- 这也不起作用:当我将 2 $node-Lines 更改为此代码时:

$node = &$input;
[...]
$node = &$node[$keys[$i]];  //go to child-node

但这不起作用,因为我只想导航到子节点的第二行更改了父元素(因为 $node 是参考)。

我不知道是否有这样做的技巧,我希望有人可以帮助我

4

2 回答 2

1

好的,这是一些决定,您应该更改$rules结构,它应该与以下内容相同$input

$input = json_decode('[{"meta":{"title": "Adressdata","name": "adress"},"data":{"desc":{"tooltip":"text","maxlength":"100"},"line1":{"tooltip":"Recipient","maxlength":"40"}}}]',true);
$rules = json_decode('[{"meta":{"title": "Companyaddress"},"data":{"desc":{"tooltip":"Another Text","maxlength":"150"}}}]',true);

 // if you print both arrays you will see that they have the similar structure
 // but $rules consists only of items that you need to change.
 // next:
 echo'<pre>',print_r(array_replace_recursive($input, $rules)),'</pre>';

 // BINGO!
于 2013-07-28T16:32:27.340 回答
0

好的,经过一些令人沮丧的小时后,我终于找到了实现这一目标的方法。

这是我写的函数:

/**
 * Manipulates an array according to the given rules
 */
function manipulateData($input, $manipulations){

    foreach ($manipulations as $target => $replacement) {   //for each rule 
        $tmpRefNum = 0;                              //Variable-Nameprefix for the reference (don't generate random names)

        $node = "node".(++$tmpRefNum);               //"node1"
        $$node = &$input;                            //$node1 --> $input

        foreach (explode(">",$target) as $index) {   //for each child in the rule           
            $parent = &$$node;                       //We search for a child. So the old node becomes a parent

            if(!isset($parent[$index])){             //Does the child exist?        no --> create missing child             
                $parent[$index] = "";                //give birth! (value will be set later)                
            }

            $node = "node".(++$tmpRefNum);           //Generate a new reference-Name: "node2", "node3", ...
            $$node = &$parent[$index];               //Point to child        
        }

        $$node = $replacement;                       //Change the child-content

        //Unset all generated references (just to not spam the variables)
        /* */   for($i=1;$i<=$tmpRefNum;$i++){
        /* */       $node = "node".$i;
        /* */       unset($$node);
        /* */   }
        /* */   
    }
    return $input;
}

描述:

解决方案是,我只为找到的每个子节点生成一个新变量/引用。为此,我需要 php 提供的 $$-Syntax。

这有点脏,因为我有一个变量工厂,所以如果有人找到更好的解决方案,那就太棒了。

如果这是一个好的解决方案,如果我能得到一些反馈,那就太好了。

于 2013-07-28T16:30:56.837 回答