PHP替换数组中的数组值

3

我有一个通过array_merge和json编码得到的结果数组,看起来像下面这样:

$c=[{"type":"textarea","label":"textarea","Element_Values":"cfgedrgyte","Element_Name":"textarea-201859","Count_Images":0,"req":0},  {"type":"dropdown","label":"dropdownbox","Element_Values":"d2","Element_Name":"dropdown-480200","Count_Images":0,"req":0,"choices":[{"label":"d1","sel":0},{"label":"d2","sel":0}]},
    {"type":"sub-header","label":"section3","Element_Values":"-","Element_Name":"sub-header-327336","Count_Images":0,"req":0},{"type":"checkbox","label":"checkbox","Element_Values":"[\"c1\",\"c2\"]","Element_Name":"checkbox-483738","Count_Images":0,"req":0,"choices":[{"label":"c1","sel":0},{"label":"c2","sel":0}]}, {"type":"radio","label":"radio","Element_Values":"\"r2\"","Element_Name":"radio-824113","Count_Images":0,"req":0,"choices":[{"label":"r1","sel":0}, {"label":"r2","sel":0}]},
    {"type":"description","label":"test template is here","Element_Values":"-","Element_Name":"description-764196","Count_Images":0,"req":0}]

如果类型是复选框或单选框,请获取Element_Values并根据Element_Values更改数组选项choices。
{"type":"checkbox","label":"checkbox","Element_Values":"[\"c1\",\"c2\"]","Element_Name":"checkbox-483738","Count_Images":0,"req":0,"choices":[{"label":"c1","sel":0},{"label":"c2","sel":0}]}

上面,元素值为c1和c2。然后我需要将sel更改为1。
"choices":[{"label":"c1","sel":1},{"label":"c2","sel":1}]}

这是另一种类型的单选按钮:

{"type":"radio","label":"radio","Element_Values":"\"r2\"","Element_Name":"radio-824113","Count_Images":0,"req":0,"choices":[{"label":"r1","sel":0},{"label":"r2","sel":0}]}

输出应该是:

{"type":"radio","label":"radio","Element_Values":"\"r2\"","Element_Name":"radio-824113","Count_Images":0,"req":0,"choices":[{"label":"r1","sel":0},{"label":"r2","sel":1}]}

我做了以下代码:
$c=json_decode($c);
foreach($c as $kc=>&$vc)
{
    if( $vc['type']=="checkbox" || $vc['type']=="radio")
    {
       $Val=$vc['Element_Values'];
       $choices=&$vc['choices'];
       foreach($choices as $key=>&$val)
       {
           if($val['label']==$Val)
           {
               $val['sel']=1;
           }
           unset($val['sel']);
       }
    }
}
echo json_encode($c);

我知道我缺少的是一个简单的东西。请帮助我解决这个问题。先谢谢了!


可能是PHP foreach更改原始数组值的重复问题。 - But those new buttons though..
2个回答

2

给你(我添加了一些注释以指出更改):

$c=<<<JSON
[{"type":"textarea","label":"textarea","Element_Values":"cfgedrgyte","Element_Name":"textarea-201859","Count_Images":0,"req":0},  {"type":"dropdown","label":"dropdownbox","Element_Values":"d2","Element_Name":"dropdown-480200","Count_Images":0,"req":0,"choices":[{"label":"d1","sel":0},{"label":"d2","sel":0}]},
    {"type":"sub-header","label":"section3","Element_Values":"-","Element_Name":"sub-header-327336","Count_Images":0,"req":0},{"type":"checkbox","label":"checkbox","Element_Values":"[\"c1\",\"c2\"]","Element_Name":"checkbox-483738","Count_Images":0,"req":0,"choices":[{"label":"c1","sel":0},{"label":"c2","sel":0}]}, {"type":"radio","label":"radio","Element_Values":"\"r2\"","Element_Name":"radio-824113","Count_Images":0,"req":0,"choices":[{"label":"r1","sel":0}, {"label":"r2","sel":0}]},
    {"type":"description","label":"test template is here","Element_Values":"-","Element_Name":"description-764196","Count_Images":0,"req":0}]
JSON;

//add second argument here
$c=json_decode($c,true);
//by refrence here
foreach($c as $kc=>&$vc)
{
    //in array is prettier, if you want to expand on this it will be easier
    //instead of another OR you just add another element, a switch would work too
    if( in_array($vc['type'], ["checkbox","radio"]))
    {
       $Val=$vc['Element_Values'];
       //This is double encoded.  Sometimes its an array sometimes it's not
       //So normailize it 
       if(gettype($Val) == 'string'){
            //decode it if it's not an array
            $Val = json_decode($Val);
            //normalize to array
            if($Val && !is_array($Val)) $Val = [$Val];

       }
       $choices =& $vc['choices']; //by refrence here

       foreach($choices as $key=>&$val)
       {

           if(in_array($val['label'],$Val)) //Use in array
           {
               $val['sel']=1;
           }
          // unset($val['sel']); whats this for? it just unsets the above
       }
    }
}
print_r($c);
echo json_encode($c);

输出

Array
(
    [0] => Array
        (
            [type] => textarea
            [label] => textarea
            [Element_Values] => cfgedrgyte
            [Element_Name] => textarea-201859
            [Count_Images] => 0
            [req] => 0
        )

    [1] => Array
        (
            [type] => dropdown
            [label] => dropdownbox
            [Element_Values] => d2
            [Element_Name] => dropdown-480200
            [Count_Images] => 0
            [req] => 0
            [choices] => Array
                (
                    [0] => Array
                        (
                            [label] => d1
                            [sel] => 0
                        )

                    [1] => Array
                        (
                            [label] => d2
                            [sel] => 0
                        )

                )

        )

    [2] => Array
        (
            [type] => sub-header
            [label] => section3
            [Element_Values] => -
            [Element_Name] => sub-header-327336
            [Count_Images] => 0
            [req] => 0
        )

    [3] => Array
        (
            [type] => checkbox
            [label] => checkbox
            [Element_Values] => ["c1","c2"]
            [Element_Name] => checkbox-483738
            [Count_Images] => 0
            [req] => 0
            [choices] => Array
                (
                    [0] => Array
                        (
                            [label] => c1
                            [sel] => 1
                        )

                    [1] => Array
                        (
                            [label] => c2
                            [sel] => 1
                        )

                )

        )

    [4] => Array
        (
            [type] => radio
            [label] => radio
            [Element_Values] => "r2"
            [Element_Name] => radio-824113
            [Count_Images] => 0
            [req] => 0
            [choices] => Array
                (
                    [0] => Array
                        (
                            [label] => r1
                            [sel] => 0
                        )

                    [1] => Array
                        (
                            [label] => r2
                            [sel] => 1
                        )

                )

        )

    [5] => Array
        (
            [type] => description
            [label] => test template is here
            [Element_Values] => -
            [Element_Name] => description-764196
            [Count_Images] => 0
            [req] => 0
        )

)

Json

[{"type":"textarea","label":"textarea","Element_Values":"cfgedrgyte","Element_Name":"textarea-201859","Count_Images":0,"req":0},{"type":"dropdown","label":"dropdownbox","Element_Values":"d2","Element_Name":"dropdown-480200","Count_Images":0,"req":0,"choices":[{"label":"d1","sel":0},{"label":"d2","sel":0}]},{"type":"sub-header","label":"section3","Element_Values":"-","Element_Name":"sub-header-327336","Count_Images":0,"req":0},{"type":"checkbox","label":"checkbox","Element_Values":"[\"c1\",\"c2\"]","Element_Name":"checkbox-483738","Count_Images":0,"req":0,"choices":[{"label":"c1","sel":1},{"label":"c2","sel":1}]},{"type":"radio","label":"radio","Element_Values":"\"r2\"","Element_Name":"radio-824113","Count_Images":0,"req":0,"choices":[{"label":"r1","sel":0},{"label":"r2","sel":1}]},{"type":"description","label":"test template is here","Element_Values":"-","Element_Name":"description-764196","Count_Images":0,"req":0}]

沙盒

更新

在示例数据中,此元素始终是双重编码的。 $ Val = $ vc ['Element_Values'];第一次我做它时,单选按钮丢失了。 因此,现在如果它不是数组,则必须对其进行解码,然后检查结果是否为数组。 单选按钮的值是"\"r2\"",如果没有解码,则具有多余的引号'"r2"',但解码后是一个字符串。 所以我们不需要担心2种数据类型,所以我们可以将其变成数组...

干杯

奖金

这里不需要if:

if( in_array($vc['type'], ["checkbox","radio"]))

我将其更改为in_array,因为这样更容易添加。

if( in_array($vc['type'], ["checkbox","radio","drowdown"]))
//instead of
if( $vc['type']=="checkbox" || $vc['type']=="radio" || $vc['type']=="drowdown")

但交换机也很好:

switch($vc['type']){
    case 'checkbox':
    case 'radio':
      //... Code ...
    break;
}

如果您想为其他类型添加代码:

switch($vc['type']){
    case 'checkbox':
    case 'radio':
      //... Code ...
    break;
    case 'dropdown':
       //... Code ...
    break;
}

在我的看法中,使用in_array或switch会更加简洁。


这段代码适用于复选框类型,但对于单选按钮类型仍无法更改sel值。{"type":"radio","label":"radio","Element_Values":""r2"","Element_Name":"radio-824113","Count_Images":0,"req":0,"choices":[{"label":"r1","sel":0},{"label":"r2","sel":1}]} - Zendie
这个无线电类型没问题。我也可以将其进行JSON编码。谢谢你的回答。 - Zendie
1
@Zendie - 现在单选按钮已经修复了。 - ArtisticPhoenix
1
当然,一开始我没有意识到它有额外的引号... JSON 很难读取,因此我将输出作为一个数组包含了进来。 - ArtisticPhoenix

0
如果你想改变原始数组,那么所有与它相关的变量都应该被设置为引用。
foreach($c as $kc=> &$vc)
{
       $choices= &$vc['choices'];
       foreach($choices as $key=>&$val)

如果您不想使用引用变量,您应该从顶部更改值。
foreach($choices as $key=> $val)
    $c[$kc]['choices'][$key]['sel']=1;

我想更改原始数组。但是只想根据Element_Values将sel值0更改为1。我已根据此编辑了我的代码。但是由于使用了unset,可能会丢失sel键。 - Zendie
是的,删除未设置的。 - shingo
我已经注释掉了unset行,但是值没有改变。 - Zendie
你应该检查输入数据,现在没有任何与条件匹配的内容。 - shingo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接