更改从序列化形式创建的数组中的键和值

时间:2019-01-20 15:50:18

标签: php html arrays ajax

我有一个已序列化的表单,然后使用ajax发布到PHP脚本。

发布表单时,我的数组如下所示:

Array
(
    [0] => Array
        (
            [name] => informatie[titel]
            [value] => 
        )

    [1] => Array
        (
            [name] => informatie[inspectienummer]
            [value] => 
        )

    [2] => Array
        (
            [name] => informatie[datum]
            [value] => 
        )

    [3] => Array
        (
            [name] => informatie[naaminspecteur]
            [value] => 
        )

    [4] => Array
        (
            [name] => informatie[locatie]
            [value] => 
        )

    [5] => Array
        (
            [name] => informatie[naamgeauditeerden]
            [value] => 
        )

    [6] => Array
        (
            [name] => questionlist[jjjj][hhhh]
            [value] => ok
        )

    [7] => Array
        (
            [name] => questionlist[jjjj][jjj]
            [value] => ok
        )

    [8] => Array
        (
            [name] => questionlist[Testt][test]
            [value] => fout
        )

)

该数组自动包含键namevalue。但我需要这样的数组:

Array
(
    [information] => Array
        (
            [titel] => inserted value
            [inspectienummer] => inserted value
            [datum] => inserted value
            [naaminspecteur] => inserted value
            [locatie] => inserted value
            [naamgeauditeerden] => inserted value
        )

    [questionlist] => Array
        (
            [jjjj] => Array
                    (
                        [hhhh] => ok
                        [jjj] => fout
                    )
            [testt] => Array
                    (
                        [test] => n.v.t.
                    )
        )
)

我该如何实现?这是我试图做的:

$wpi = $_POST['wpi'];


$newarray = array();
foreach($wpi as $wpikey => $wpivalue){
  $newarray[$wpikey] = $wpivalue;
}

echo '<pre>';
print_r($newarray);
echo '</pre>';

但这给了我相同的结果。

我的HTML(如果可以澄清):

<form class="addwerkplekinspectie" action="#" method="post">
    <div class="row">
        <div class="col-md-3">
            <p class="inputtitles">Werkplekinspectie template:</p>
        </div>
        <div class="col-md-9">
            <p class="inputtitles nomarginleft">jjjj</p>
        </div>
        <div class="col-md-3">
            <p class="inputtitles">Titel:</p>
        </div>
        <div class="col-md-9">
            <input type="text" name="informatie[titel]" class="form-control name_list">
        </div>
        <div class="col-md-3">
            <p class="inputtitles">Inspectienummer:</p>
        </div>
        <div class="col-md-9">
            <input type="text" name="informatie[inspectienummer]" class="form-control name_list">
        </div>
        <div class="col-md-3">
            <p class="inputtitles">Inspectiedatum:</p>
        </div>
        <div class="col-md-9">
            <input type="date" name="informatie[datum]" class="form-control name_list">
        </div>
        <div class="col-md-3">
            <p class="inputtitles">Naam inspecteur:</p>
        </div>
        <div class="col-md-9">
            <input type="text" name="informatie[naaminspecteur]" class="form-control name_list">
        </div>
        <div class="col-md-3">
            <p class="inputtitles">Locatie/Vestiging</p>
        </div>
        <div class="col-md-9">
            <input type="text" name="informatie[locatie]" class="form-control name_list">
        </div>
        <div class="col-md-3">
            <p class="inputtitles">Naam/ Namen geauditeerden</p>
        </div>
        <div class="col-md-9">
            <input type="text" name="informatie[naamgeauditeerden]" class="form-control name_list">
        </div>
    </div>

    <label class="categorytitle">jjjj</label>
    <div class="row">
        <div class="col-md-8">
            <p class="questionclass">hhhh</p>
        </div>
        <div class="col-md-4">
            <div class="container text-right">
                <input type="radio" name="questionlist[jjjj][hhhh]" id="radio-1" value="ok" required>
                <label class="radiotoggle" for="radio-1"><span class="radio">Ok</span></label>
                <input type="radio" name="questionlist[jjjj][hhhh]" id="radio-2" value="fout">
                <label class="radiotoggle" for="radio-2"><span class="radio">Fout</span></label>
                <input type="radio" name="questionlist[jjjj][hhhh]" id="radio-3" value="nvt">
                <label class="radiotoggle" for="radio-3"><span class="radio">N.v.t</span></label>
            </div>
        </div>
        <div class="col-md-8">
            <p class="questionclass">jjj</p>
        </div>
        <div class="col-md-4">
            <div class="container text-right">
                <input type="radio" name="questionlist[jjjj][jjj]" id="radio-4" value="ok" required>
                <label class="radiotoggle" for="radio-4"><span class="radio">Ok</span></label>
                <input type="radio" name="questionlist[jjjj][jjj]" id="radio-5" value="fout">
                <label class="radiotoggle" for="radio-5"><span class="radio">Fout</span></label>
                <input type="radio" name="questionlist[jjjj][jjj]" id="radio-6" value="nvt">
                <label class="radiotoggle" for="radio-6"><span class="radio">N.v.t</span></label>
            </div>
        </div>
    </div>
    <label class="categorytitle">Testt</label>
    <div class="row">
        <div class="col-md-8">
            <p class="questionclass">test</p>
        </div>
        <div class="col-md-4">
            <div class="container text-right">
                <input type="radio" name="questionlist[Testt][test]" id="radio-7" value="ok" required>
                <label class="radiotoggle" for="radio-7"><span class="radio">Ok</span></label>
                <input type="radio" name="questionlist[Testt][test]" id="radio-8" value="fout">
                <label class="radiotoggle" for="radio-8"><span class="radio">Fout</span></label>
                <input type="radio" name="questionlist[Testt][test]" id="radio-9" value="nvt">
                <label class="radiotoggle" for="radio-9"><span class="radio">N.v.t</span></label>
            </div>
        </div>
    </div>
</form>

1 个答案:

答案 0 :(得分:1)

尝试一下。

//container for new array
$arr2 = [];

//loop over the outer parts of the array...
foreach($wpi as $sub_arr) {

    //...parse the namespace parts in the name field
    preg_match_all('/^[^\[]+|(?<=\[)[^\]]+(?=\])/', $sub_arr['name'], $parts);

    //...recursively add the values to a new, multi-dimensionsl array
    $append_to = &$arr2;
    foreach($parts[0] as $i => $part) {
        if (!array_key_exists($part, $append_to))
            $append_to[$part] = $i < count($parts[0]) - 1 ? [] : $sub_arr['value'];
        $append_to = &$append_to[$part];
    }
}