0

クエリ文字列

    name=form1

    &settings={"en":{"name":"Form 1","classes":["leftAlign"],"heading":"h2","styles":{"fontFamily":"default","fontSize":"default","fontStyles":[1,0,0]}},"styles":{"color":"default","backgroundColor":"default"}}

    &fields[0].id=null&fields[0].name=password1&fields[0].type=Password&fields[0].settings={"en":{"label":"Password 1","value":"","description":"","styles":{"fontFamily":"default","fontSize":"default","fontStyles":[0,0,0]}},"_persistable":true,"required":true,"restriction":"no","styles":{"label":{"color":"default","backgroundColor":"default"},"value":{"color":"default","backgroundColor":"default"},"description":{"color":"777777","backgroundColor":"default"}}}&fields[0].sequence=0&fields[0].status=

........ 

これを配列出力に変換する必要があります。クエリ文字列を配列に解析するためにいくつかのメソッドを使用しました。これは私が得た出力です。ただし、配列の「フィールド」の値は表示されていません。それを取得する他の方法はありますか?field [0]、fields [1]、fields [2]にはキーと値がありますが、表示されていません。

Array
(
    [name] => form1
    [settings] => {"en":{"name":"Form 1","classes":["leftAlign"],"heading":"h2","styles":{"fontFamily":"default","fontSize":"default","fontStyles":[1,0,0]}},"styles":{"color":"default","backgroundColor":"default"}}
    [fields] => Array
        (
            [0] => 
            [1] => 
            [2] => 
        )

    [create] => Create
)
4

2 に答える 2

1
<?php
 $a = explode('&', $QUERY_STRING); 
 $i = 0; 
 $field = array();
 while ($i < count($a)) {
    $b = split('=', $a[$i]); 
    field[i]= htmlspecialchars(urldecode($b[1])); 
    $i++;
 } ?>
于 2012-09-01T07:10:20.130 に答える
1

更新されたコード

$query = 'name=form1'
        . '&settings={"en":{"name":"Form 1","classes":["leftAlign"],"heading":"h2","styles":{"fontFamily":"default","fontSize":"default","fontStyles":[1,0,0]}},"styles":{"color":"default","backgroundColor":"default"}}'
        . '&fields[0].id=null&fields[0].name=password1&fields[0].type=Password&fields[0].settings={"en":{"label":"Password 1","value":"","description":"","styles":{"fontFamily":"default","fontSize":"default","fontStyles":[0,0,0]}},"_persistable":true,"required":true,"restriction":"no","styles":{"label":{"color":"default","backgroundColor":"default"},"value":{"color":"default","backgroundColor":"default"},"description":{"color":"777777","backgroundColor":"default"}}}&fields[0].sequence=0&fields[0].status='
        //adding fields[1]
        . "&fields[1].id=null&fields[1].name=f1name&fields[1].type=f1type";

$resultArray = array();
foreach (explode('&', $query) as $pair) {
    list($key, $value) = explode('=', $pair);

    //a dot present
    if (strpos($key, '.') !== false) {
        list($subKey, $subVal) = explode('.', $key);

        if (preg_match('/(?P<name>\w+)\[(?P<index>\w+)\]/', $subKey, $matches)) {
            $resultArray[$matches['name']][$matches['index']][$subVal] = $value;
        } else {
            $resultArray[$subKey][$subVal] = $value;
        }
    } else {
        $resultArray[$key] = $value;
    }
}

echo '<pre>' . print_r($resultArray, true) . '</pre>';

出力

Array
(
    [name] => form1
    [settings] => {"en":{"name":"Form 1","classes":["leftAlign"],"heading":"h2","styles":{"fontFamily":"default","fontSize":"default","fontStyles":[1,0,0]}},"styles":{"color":"default","backgroundColor":"default"}}
    [fields] => Array
        (
            [0] => Array
                (
                    [id] => null
                    [name] => password1
                    [type] => Password
                    [settings] => {"en":{"label":"Password 1","value":"","description":"","styles":{"fontFamily":"default","fontSize":"default","fontStyles":[0,0,0]}},"_persistable":true,"required":true,"restriction":"no","styles":{"label":{"color":"default","backgroundColor":"default"},"value":{"color":"default","backgroundColor":"default"},"description":{"color":"777777","backgroundColor":"default"}}}
                    [sequence] => 0
                    [status] => 
                )

            [1] => Array
                (
                    [id] => null
                    [name] => f1name
                    [type] => f1type
                )

        )

)
于 2012-09-01T07:27:20.630 に答える