PHP替换数组内的数组值

时间:2019-06-24 02:24:50

标签: 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}]}

这是另一种类型的收音机:

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

我知道这很简单,我很想念。请帮我解决这个问题。预先感谢!

2 个答案:

答案 0 :(得分: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}]

Sandbox

更新

在示例数据中,此元素始终被双重编码。 $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;
}

使用阵列或交换机只是更清洁的IMO。

答案 1 :(得分: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;