dsgdg54ef4365 2019-06-23 18: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-23 19: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部