如何通过爆炸并将匹配的键值排列成一个键来创建键值?

I am recently facing a practical problem.I am working with ajax form submission and there has been some checkboxes.I need all checkboxes with same name as key value pair.Suppose there is 4 checkboxes having name attribute =checks so i want something like $arr['checks'] = array(value1, value2, ...)

So i am getting my ajax $_POST code as suppose like: name=alex&checks=code1&checks=code2&checks=code3

I am using below code to make into an array

public function smdv_process_option_data(){
    $dataarray = array();
    $newdataarray = array();
    $new = array();
    $notices = array();
    $data = $_POST['options']; // data received by ajax

    $dataarray = explode('&', $data);
    foreach ($dataarray as $key => $value) {
        $i = explode('=', $value);
        $j = 1;
        if(array_key_exists($i[0], $newdataarray)){
            if( !is_array($newdataarray[$i[0]]) ){
                array_push($new, $newdataarray[$i[0]]);

            }else{
                array_push($new, $i[1]);

            }
            $newdataarray[$i[0]] = $new;
        }else{
            $newdataarray[$i[0]] = $i[1];
        }

    }


    die($newdataarray);
}

Here i want $newdataarray as like below

array(
   'name' => 'alex',
   'checks => array(code1, code2, code3),
)

But any how I am missing 2nd value from checks key array.

As I see it you only need to do two explode syntaxes.
The first on is to get the name and here I explode on & and then on name= in order to isolate the name in the string.
The checks is an explode of &checks= if you omit the first item with array_slice.

$str = 'name=alex&checks=code1&checks=code2&checks=code3';

$name = explode("name=", explode("&", $str)[0])[1];
// alex

$checks = array_slice(explode("&checks=", $str), 1);
// ["code1","code2","code3"]

https://3v4l.org/TefuG

So i am getting my ajax $_POST code as suppose like: name=alex&checks=code1&checks=code2&checks=code3

Use parse_str instead.

https://php.net/manual/en/function.parse-str.php

parse_str ( string $encoded_string [, array &$result ] ) : void Parses encoded_string as if it were the query string passed via a URL and sets variables in the current scope (or in the array if result is provided).

$s = 'name=alex&checks=code1&checks=code2&checks=code3';

parse_str($s, $r);

print_r($r);

Output

Array
(
    [name] => alex
    [checks] => code3
)

You may think this is wrong because there is only one checks but technically the string is incorrect.

Sandbox

You shouldn't have to post process this data if it's sent correctly, as that is not included in the question, I can only make assumptions about it's origin.

If your manually creating it, I would suggest using serialize() on the form element for the data for AJAX. Post processing this is just a band-aid and adds unnecessary complexity.

If it's from a source outside your control, you'll have to parse it manually (as you attempted).

For example the correct way that string is encoded is this:

name=alex&checks[]=code1&checks[]=code2&checks[]=code3

Which when used with the above code produces the desired output.

Array
(
    [name] => alex
    [checks] => Array
        (
            [0] => code1
            [1] => code2
            [2] => code3
        )

)

So is the problem here, or in the way it's constructed...

UPDATE

I felt obligated to give you the manual parsing option:

$str = 'name=alex&checks=code1&checks=code2&checks=code3';
$res = [];

foreach(explode('&',$str) as $value){
    //PHP 7 array destructuring 
    [$key,$value] = explode('=', $value);

    //PHP 5.x list()
    //list($key,$value) = explode('=', $value);

    if(isset($res[$key])){
        if(!is_array($res[$key])){
            //convert single values to array
            $res[$key] = [$res[$key]];
        }
        $res[$key][] = $value;
    }else{
        $res[$key] = $value;
    }
}

print_r($res);

Sandbox

The above code is not specific to your keys, which is a good thing. And should handle any string formatted this way. If you do have the proper array format mixed in with this format you can add a bit of additional code to handle that, but it can become quite a challenge to handle all the use cases of key[] For example these are all valid:

 key[]=value&key[]=value //[key => [value,value]]
 key[foo]=value&key[bar]=value //[key => [foo=>value,bar=>value]]
 key[foo][]=value&key[bar][]=value&key[bar][]=value //[key => [foo=>[value]], [bar=>[value,value]]]

As you can see that can get out of hand real quick, so I hesitate to try to accommodate that if you don't need it.

Cheers!