PHP 替换数组中的数组值

标签 php arrays

我有一个来自 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 更改数组选择。

{"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}]}

另一种 radio 是:

{"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);

我知道这是我想念的一件简单的事情。请帮我解决这个问题。提前致谢!

最佳答案

给你(我放了一些评论来指出变化):

$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}]

Sandbox

更新

在示例数据中,此元素始终是双重编码的。 $Val=$vc['Element_Values']; 我第一次这样做时缺少单选按钮。所以现在我让它解码它,如果它不是一个数组,然后检查结果是否是一个数组。 radio 值是这个 "\"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 会更干净一些。

关于PHP 替换数组中的数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56729191/

相关文章:

php - 使用 php mysql 在巨型多维数组中搜索

ios - 字典数组中每个对象的最大值

arrays - Pharo 3 中文字数组和动态数组的区别

php - 正则表达式 PHP ,邮政地址

javascript - 复制外部 json 数据并粘贴到本地 json 文件中

php - 作业调度问题

php - PHP中使用逻辑运算符的变量赋值

javascript - ES6 类默认值与数组

c - 尝试删除 C 中字符数组中的最后一个字符

php - 如何通过 php 保存用户对 mysql 评论的投票?