Print $_POST variable name along with value - php

I have a POST in PHP for which I won't always know the names of the variable fields I will be processing.
I have a function that will loop through the values (however I would also like to capture the variable name that goes with it.)
foreach ($_POST as $entry)
{
print $entry . "<br>";
}
Once I figure out how to grab the variable names, I also need to figure out how I can make the function smart enough to detect and loop through arrays for a variable if they are present (i.e. if I have some checkbox values.)

If you just want to print the entire $_POST array to verify your data is being sent correctly, use print_r:
print_r($_POST);
To recursively print the contents of an array:
printArray($_POST);
function printArray($array){
foreach ($array as $key => $value){
echo "$key => $value";
if(is_array($value)){ //If $value is an array, print it as well!
printArray($value);
}
}
}
Apply some padding to nested arrays:
printArray($_POST);
/*
* $pad='' gives $pad a default value, meaning we don't have
* to pass printArray a value for it if we don't want to if we're
* happy with the given default value (no padding)
*/
function printArray($array, $pad=''){
foreach ($array as $key => $value){
echo $pad . "$key => $value";
if(is_array($value)){
printArray($value, $pad.' ');
}
}
}
is_array returns true if the given variable is an array.
You can also use array_keys which will return all the string names.

You can have the foreach loop show the index along with the value:
foreach ($_POST as $key => $entry)
{
print $key . ": " . $entry . "<br>";
}
As to the array checking, use the is_array() function:
foreach ($_POST as $key => $entry)
{
if (is_array($entry)) {
foreach($entry as $value) {
print $key . ": " . $value . "<br>";
}
} else {
print $key . ": " . $entry . "<br>";
}
}

It's much better to use:
if (${'_'.$_SERVER['REQUEST_METHOD']}) {
$kv = array();
foreach (${'_'.$_SERVER['REQUEST_METHOD']} as $key => $value) {
$kv[] = "$key=$value";
}
}

If you want to detect array fields use a code like this:
foreach ($_POST as $key => $entry)
{
if (is_array($entry)){
print $key . ": " . implode(',',$entry) . "<br>";
}
else {
print $key . ": " . $entry . "<br>";
}
}

Related

iterate associate array in php does not print out values

I'm trying out the following code for arrays in php, I create a associate array, print out the values and add one more to the array - print out again. This works, but if I try the foreach ($MovieCollection as $key => $value) it does not print out the values. Why does it not do that?
$myArray = array("Star Wars", "The Shining");
foreach ($myArray as $val)
{
echo("Movie: " . $val ."<br>");
}
$MovieCollection = array();
$MovieCollection[] = array('title' => 'Star Wars', 'description' =>'classic');
foreach ($MovieCollection as $film )
{
echo($film['title'] .": " . $film['description'] ."<br>");
}
$MovieCollection[] = array('title' => 'The shinning', 'description' =>'creepy');
foreach ($MovieCollection as $film )
{
echo($film['title'] .": " . $film['description'] ."<br>");
}
echo("<br><br>");
// This does not print the values?
foreach ($MovieCollection as $key => $value)
{
echo($key .": " . $value ."<br>");
}
That is because in this part $MovieCollection is an array of arrays and if you want to echo the $value which is an array, you will do an Array to string conversion which does not work.
What you might do is use another foreach to show the values per array:
foreach ($MovieCollection as $value) {
foreach ($value as $k => $v) {
echo($k .": " . $v ."<br>");
}
}
See a Php demo

retrieving array from mysql and iterating through that array

below is a php object which is retrieving some values from mysql db through a php method
$query = "SELECT imgpath from images";
$oMySQL->ExecuteSQL($query);
Now when i use this
$result=$oMySQL->ExecuteSQL($query);
it prints "Array"
How to iterate through the array
Regards Jane
A simple foreach loop.
foreach ($result as $key => $val) {
foreach ($val as $label => $item) {
echo $label . " - " . $item;
}
}
$key will hold the associative name the array element, and $val will hold the value of the element.
You mean it print array when you do echo $result?
Then:
foreach($result as $index => $value) {
echo $index . '=' $value;
}

get all values from php associative array

I have an array $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);. How can i get (echo) all names and corresponding ages in a single instance (like foreach $value as $value)?? The array may have more data than shown here.
foreach ($ages as $name => $age) {
echo "$name = $age\n";
}
u have a lot options
like
print_r
print_r($array)
var_dump , foreach
print_r($array);
or
var_dump($array)
foreach ($ages as $key => $value) {
echo $key . "'s age is " . $value . "<br />";
}

Print contents of an array and their key in PHP

so currently I have a PHP function that prints out the applications configuration:
function cconfig() {
global $config;
// If the user wants debug information shown:
if (SHOWDEBUG == true) {
// If there is config information to show
if (isset($config)) {
cout("Variable $config is set.", "debug");
foreach ($config as $current) {
echo $current."<br/>";
}
} else {
cout("Variable $config isn't set.", "debug");
}
}
}
The output only shows the values of the array that it's at $current. For example:
Value1
Value2
Value3
How can I edit this function so instead of just showing the value at a given key, it also shows the key?
ConfigEntry1 = Value1
ConfigEntry2 = Value2
ConfigEntry3 = Value3
foreach ($config as $key => $current) {
echo "$key = $current<br />";
}
Expose the key like so:
foreach ($config as $k => $v) {
echo $k. ' ' . $v ."<br/>";
}
The two forms a foreach can take, from the manual:
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
The first form loops over the array
given by array_expression. On each
loop, the value of the current element
is assigned to $value and the internal
array pointer is advanced by one (so
on the next loop, you'll be looking at
the next element).
The second form does the same thing, except that the current element's key
will be assigned to the variable $key
on each loop.
Change:
foreach ($config as $current) {
echo $current."<br/>";
}
to
foreach ($config as $key => $val) {
echo $key." = " . $val . "<br/>";
}

echo key and value of an array without and with loop

This is an array i have
<?php
$page['Home']='index.html';
$page['Service']='services.html';
?>
How do i get to echo something like this for individual one like
Home is at index.html
and again how can i do this through a loop and echo all?
foreach($page as $key => $value) {
echo "$key is at $value";
}
For 'without loop' version I'll just ask "why?"
Without a loop, just for the kicks of it...
You can either convert the array to a non-associative one, by doing:
$page = array_values($page);
And then acessing each element by it's zero-based index:
echo $page[0]; // 'index.html'
echo $page[1]; // 'services.html'
Or you can use a slightly more complicated version:
$value = array_slice($page, 0, 1);
echo key($value); // Home
echo current($value); // index.html
$value = array_slice($page, 1, 1);
echo key($value); // Service
echo current($value); // services.html
If you must not use a loop (why?), you could use array_walk,
function printer($v, $k) {
echo "$k is at $v\n";
}
array_walk($page, "printer");
See http://www.ideone.com/aV5X6.
Echo key and value of an array without and with loop
$array = array(
'kk6NFKK'=>'name',
'nnbDDD'=>'claGg',
'nnbDDD'=>'kaoOPOP',
'nnbDDD'=>'JWIDE4',
'nnbDDD'=>'lopO'
);
print_r(each($array));
Output
Array
(
[1] => name
[value] => name
[0] => kk6NFKK
[key] => kk6NFKK
)
for the first question
$key = 'Home';
echo $key." is at ".$page[$key];
function displayArrayValue($array,$key) {
if (array_key_exists($key,$array)) echo "$key is at ".$array[$key];
}
displayArrayValue($page, "Service");
How to echo key and value of an array without and with loop
$keys = array_keys($page);
implode(',',$keys);
echo $keys[0].' is at '.$page['Home'];
My version without a loop would be like this:
echo implode(
"\n",
array_map(
function ($k, $v) {
return "$k is at $v";
},
array_keys($page),
array_values($page)
)
);
array_walk($v, function(&$value, $key) {
echo $key . '--'. $value;
});
Learn more about array_walk
A recursive function for a change;) I use it to output the media information for videos etc elements of which can use nested array / attributes.
function custom_print_array($arr = array()) {
$output = '';
foreach($arr as $key => $val) {
if(is_array($val)){
$output .= '<li><strong>' . ucwords(str_replace('_',' ', $key)) . ':</strong><ul class="children">' . custom_print_array($val) . '</ul>' . '</li>';
}
else {
$output .= '<li><strong>' . ucwords(str_replace('_',' ', $key)) . ':</strong> ' . $val . '</li>';
}
}
return $output;
}
You can try following code:
foreach ($arry as $key => $value)
{
echo $key;
foreach ($value as $val)
{
echo $val;
}
}

Categories