2016-04-21 29 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