3

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!

Zendie
  • 1,176
  • 1
  • 13
  • 30

2 Answers2

2

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.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • This code works fo the type checkbox, but for the type radio its still not changing the sel value. {"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 Jun 24 '19 at 03:52
  • Thats ok with radio type. I can json encode it as well. Thank you for the answer – Zendie Jun 24 '19 at 03:58
  • 1
    @Zendie - it's fixed for radio buttons now. – ArtisticPhoenix Jun 24 '19 at 04:00
  • 1
    Sure, I didn't realize that it had the extra quotes at first... Hard to read the JSON so I included the output as an array. – ArtisticPhoenix Jun 24 '19 at 04:05
0

If you want to change the original array, all the variables from it should be set as a reference.

foreach($c as $kc=> &$vc)
{
       $choices= &$vc['choices'];
       foreach($choices as $key=>&$val)

If you don't want to use a reference variable, you should change the value from the top

foreach($choices as $key=> $val)
    $c[$kc]['choices'][$key]['sel']=1;
shingo
  • 18,436
  • 5
  • 23
  • 42