2016-04-21 37 views
0

爲了編寫發送到頁面的所有HTTP變量並將其寫入調試文件,我希望能夠從其子節點命名「父」數組。假設我有這樣的代碼(並且該頁面遠程調用):從「子」中獲取「父」數組的名稱

$father = array (getallheaders(), $_POST, $_GET); 
$info = ''; 
foreach ($father as $child){ 
    $info .= ${"child"} . "\n"; 
    $info .= '--------------' . "\n"; 
    foreach ($child as $key => $val){ 
    $info .= $key . ' : ' . $val . "\n"; 
    } 
    $info .= "\n\n"; 
} 

//write $info to a debug file 

就是我希望做到的,是包含以下信息調試文件:

getallheaders() 
-------------- 
Host : 1.2.3.4 
Connection : keep-alive 
// all other members of getallheaders() array 

$_POST 
-------------- 
// assuming that page was called via HTTP POST 
INPUT1 : input one text 
INPUT2 : input two text 
// all other members of $_POST array 

$_GET 
-------------- 
// assuming that page was called via HTTP GET 
INPUT10 : input ten text 
INPUT11 : input eleven text 
// all other members of $_GET array 
... 

等。 ..

此刻,我得到了我想要的調試文件中的所有信息,但我目前正在使用的父數組的「名稱」僅顯示爲Array:這使得總體感,但我無法弄清楚如何得到它的名字並將其顯示爲字符串值。這是調試文件的內容:

Array 
-------------- 
Host : 1.2.3.4 
Connection : keep-alive 
// all other members of getallheaders() array 

Array 
-------------- 
// assuming that page was called via HTTP POST 
INPUT1 : input one text 
INPUT2 : input two text 
// all other members of $_POST array 

Array 
-------------- 
// assuming that page was called via HTTP GET 
INPUT10 : input ten text 
INPUT11 : input eleven text 
// all other members of $_GET array 
... 

我知道我可以建立孩子的內環內的迭代,然後調用$父親[0],$父親[1],並以某種方式轉換的名稱數組轉換成字符串,但我希望有人能指引我採取更「優雅」的方式做事?

回答

2

您的數組沒有任何關於兒童的信息。設置適當的鍵:

$father = array ('getallheaders' => getallheaders(), '$_POST' => $_POST, '$_GET' => $_GET); 

然後改變你的foreach這樣:

foreach($father as $childname => $child) 
{ 
    $info .= "$childname\n"; 
    (...) 
} 
+0

你打我吧! – Webeng

+0

杜!當然......非常感謝@ fusion3k – bnoeafk