2012-09-01 108 views
0

查詢字符串查詢字符串數組在PHP

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= 

........ 

我需要將其轉換成數組輸出。我用幾個方法來解析查詢字符串數組。這是我得到的輸出。但數組的'Fields'值沒有顯示。有沒有其他的方法來獲得這個?字段[0],字段[1] &字段[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 
) 
+0

你嘗試哪些功能? – knightrider

+0

我剛剛打印$ _GET,我得到了這個輸出。同時我使用echo $ str = urldecode($ _ SERVER ['QUERY_STRING']); //獲取查詢srting $ s = parseQueryString($ str); //功能 \t function parseQueryString($ str){ \t \t $ op = array(); \t \t $ pairs = explode(「&」,$ str); \t \t的foreach($對作爲$對){ \t \t \t列表($ K,$ V)= array_map( 「urldecode」,爆炸( 「=」,$對)); \t \t \t $ op [$ k] = $ v; \t \t} \t \t return $ op; \t} reset($ s); (列表($ key,$ value)= each($ s)){ 「Key:$ key; Value:$ value
\ n」; } – Parthi04

+0

@knightrider請給點指導 – Parthi04

回答

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 
       ) 

     ) 

) 
+0

如何將字段[0]和字段[1]分組? – Parthi04

+0

@ Parthi04 - 看我更新的代碼...希望這可以幫助你... :) – rajukoyilandy

+0

偉大的代碼...它的工作。 – Parthi04

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++; 
} ?> 
+0

@ Parthi04如果這能爲你工作,請接受答案 – knightrider