开发者

How to print out the keys of an array like $_POST in PHP?

Maybe the code looks like something like this:

fore开发者_JS百科ach(...$POST){
echo $key."<br/>;
}


var_dump($_POST);

or

print_r($_POST);

You might insert a pre tag before and after for the clearer output in your browser:

echo '<pre>';
var_dump($_POST);
echo '</pre>';

And I suggest to use Xdebug. It provides an enchanted var_dump that works without pre's as well.


See the PHP documentation on foreach: http://php.net/manual/en/control-structures.foreach.php

Your code would look something like this:

foreach ($_POST as $key=>$element) {
   echo $key."<br/>";
}


Tested one liner:

echo join('<br />',array_keys($_POST));


If you want to do something with them programmatically (eg turn them into a list or table), just loop:

foreach ($_POST as $k => $v) {
  echo $k . "<br>";
}

For debugging purposes:

print_r($_POST);

or

var_dump($_POST);


And if you want full coverage of the whole array, print_r or even more detailed var_dump


$array = array_flip($array);
echo implode('any glue between array keys',$array);


Or you could just print out the array keys:

foreach (array_keys($_POST) as $key) {
    echo "$key<br/>\n";
}


Normally I would use print_r($_POST).

If using within an HTML page, it's probably worth wrapping in a <pre> tag to get a better looking output, otherwise the useful tabs and line breaks only appear in source view.

print_r() on PHP.net

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜