dsgdg54ef4365 2019-06-24 02:24 采纳率: 0%
浏览 121
已采纳

PHP替换数组中的数组值

I have a result array from array_merge and json encode and it look like below:

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

If the type is checkbox or radio, get the Element_Values and change the array choices according to 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}]}

Above, Element Values are c1 and c2. Then I need to change sel=1. That is

"choices":[{"label":"c1","sel":1},{"label":"c2","sel":1}]}

Here another type radio is:

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

And the output should be:

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

I did the following code:

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

I know it is a simple thing I am missing. Please help me to solve this. Thanks in advance!

  • 写回答

2条回答 默认 最新

  • dsyq40772 2019-06-24 03:49
    关注

    Here you go (I put some comments in to point out changes):

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

    output

    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

    UPDATE

    This element is always double encoded, in the example data. $Val=$vc['Element_Values']; It was missing the radio button the first time I did it. So now I have it decode it if its not an array and then check that the result is an array. The radio value was this "\"r2\"" Which when not decoded has an extra set of quotes '"r2"', but is a string when decoded. So we don't want to worry about 2 data types so we can just make that an array...

    Cheers

    BONUS

    Instead of an if here:

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

    I changed this to in_array as it's easier to add to,

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

    but a switch is good too:

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

    Then if you want to add code for other types:

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

    Using In array or a switch is just a bit cleaner IMO.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况