eliminate a specified post data on a post request array - php

I use this code to retrieve all the post request (refer below)
<?php
foreach ($_POST as $key => $value) {
$body .= $key . ": " . $value . '<br>';
}
echo $body;
?>
and there is a post data named "adminemail" and "cat", now what i want is to eliminate those two and print all the post data except those two. How to do that? any suggestions, recommendations and ideas, would love to hear. Thank you in advance.

option 1
unset($_POST['adminemail'],$_POST['cat']);
option 2
<?php
foreach ($_POST as $key => $value) {
if(!in_array($key,array('adminemail','cat'))){
$body .= $key . ": " . $value . '<br>';
}
}
echo $body;
?>

The following should work:
<?php
$arr = array_diff_key($_POST, array("adminemail" => 0, "cat" => 0));
$body = ""; // You must have this line, or PHP will throw an "Undefined variable" notice
foreach($arr as $key => $value){
$body .= $key . ": " . $value . "<br/>";
}
echo $body;
?>

Related

Loop through associative array and assign values

I have an array:
$instructions = array (
array("step_no"=>"1","description"=>"Ensure that you have sufficient balance"),
array("step_no"=>"2","description"=>"Approve the request sent to your phone")
);
What I want is to loop through this array, which I have done, but I am now confused because I don't know how to get the output I desire.
foreach ($array as $key => $value) {
//echo $key . "\n";
foreach ($value as $sub_key => $sub_val) {
if (is_array($sub_val)) {
//echo $sub_key . " : \n";
foreach ($sub_val as $k => $v) {
echo "\t" .$k . " = " . $v . "\n";
}
} else {
echo $sub_key . " = " . $sub_val . "\n";
}
}
}
The above code loops through the array, but this line of code:
echo $sub_key . " = " . $sub_val . "\n";
gives me:
step_no = 1 description = Ensure that you have sufficient balance step_no = 2 description = Approve the request sent to your phone
when I change it to:
echo $sub_val . "\n";
it gives me:
1 Ensure that you have sufficient balance 2 Approve the request sent to your phone
But I what I truly want is:
1. Ensure that you have sufficient balance
2. Approve the request sent to your phone
Is this possible at all? Thanks.
$instructions = array (
array("step_no"=>"1","description"=>"Ensure that you have sufficient balance"),
array("step_no"=>"2","description"=>"Approve the request sent to your phone")
);
foreach($instructions as $instruction) {
echo $instruction['step_no'] . '. ' . $instruction['description'] . "\n";
}
If it's HTML you may want to use <ol> and <li>.
It smells like you are not running this script in command line but in browser. If so, then \n makes no visual effect (unless within <pre> block) and you u must use HTML tag <br /> instead. Also, drop concatenation madness and use variable substitution:
echo "{$sub_key}. = {$sub_val}<br/>";
You can simple achieve this way
<?php
$instructions = array (
array("step_no"=>"1","description"=>"Ensure that you have sufficient balance"),
array("step_no"=>"2","description"=>"Approve the request sent to your phone")
);
foreach($instructions as $instruction){
echo $instruction['step_no'].'. '.$instruction['description'].PHP_EOL;
}
?>
Alway keep it simple.

How to remove first element of an array using loop

I have very little experience with PHP, but I'm taking a class that has PHP review exercises. One of them is to create a function that uses a loop to return all values of an array except the first value in an unordered list. I'm assuming there's a way to do this using a foreach loop but cannot figure out how. This is what I had but I feel like I am far off:
<?php
$array = array('myName' => 'Becca', 'favColor' => 'violet', 'favMovie' => 'Empire Strikes Back', 'favBook' => 'Lullaby', 'favWeb' => 'twitter.com');
$myName = $array['myName'];
$favColor = $array['favColor'];
$favMovie = $array['favMovie'];
$favBook = $array['favBook'];
$favWeb = $array['favWeb'];
echo '<h1>' . $myName . '</h1>';
function my_function() {
foreach($array == $myName){
echo '<ul>'
. '<li>' . $favColor . '</li>'
. '<li>' . $favMovie . '</li>'
. '<li>' . $favBook . '</li>'
. '<li>' . $favWeb . '</li>'
. '</ul>';
}
}
my_function();
?>
The correct syntax of foreach is
foreach (array_expression as $key => $value)
instead of
foreach($array == $myName){
function that uses a loop to return all values of an array except the
first value
I'm not sure, what exactly you mean by except the first value. If you are trying to remove first element from the array. Then you could have used array_shift
If you are supposed to use loop then
$count = 0;
foreach ($array as $key => $value)
{
if ($count!=0)
{
// your code
}
$count++;
}
Change the code to following
<?php
$array = array('myName' => 'Becca', 'favColor' => 'violet', 'favMovie' => 'Empire Strikes Back', 'favBook' => 'Lullaby', 'favWeb' => 'twitter.com');
$myName = $array['myName'];
echo '<h1>' . $myName . '</h1>';
function my_function($array)
{
$count = 0;
echo "<ul>";
foreach($array as $key => $value)
{
if($key != "myName")
{
echo "<li>".$value."</li>";
}
}
echo "</ul>";
}
my_function($array);

GET key change how to show what is in the url

I want to display everything what is in a url.
So if someone use test.php?test=yes&no=why
i want to show
test = yes
no = why
but when someone use test.php?bla=blala
i want to show
bla = blala
Is this possible?
Pretty print the $_GET variable.
<?php print_r($_GET); ?>
Try this:
foreach ($_GET as $key => $value) {
echo $key . " = " . $value
}
use this php script:
foreach($_GET as $key => $value){
echo $key . " = " . $value;
}
<?php var_dump( $_GET ); ?>
will echo out everything inside $_GET
<?php var_dump( $_REQUEST ); ?>
will do everything in both a $_POST and an $_GET
You can also try
<?php
$string = "Url Variables <br />";
foreach( $_REQUEST as $key => $value ){
$string .= $key." = ".$value." <br />";
}
echo $string;
?>

PHP displaying multidimensional array

I have this array similar to this:
$suppliers = array(
'Utility Warehouse' => array('Gas' => array(0,0), 'Electricty' => array(0,0)),
'British Gas' => array('Gas' => array(93,124), 'Electricty' => array(93,124)),
'Eon' => array('Gas' => array(93,124), 'Electricty' => array(93,124))
);
How can display the information as follows
Utility Warehouse
Gas: 0-0 Electricity 0-0
British Gas
Gas: 93-134 Electricity: 93-134
Eon
Gas: 93-124 Electricity: 93-134
You can see how the displayed data corresponds to the data in the array. I've tried this:
foreach($suppliers as $a){
echo $a[0];
}
But this does nothing. CONFUSED!
<?php
foreach($suppliers as $supplier => $category) {
echo $supplier . '<br />';
foreach($category as $cat_name => $values_arr) {
echo $cat_name . ': ' . implode('-', $values_arr) . '<br /><br />';
}
}
?>
you can try:
foreach($suppliers as $name => $value) {
echo $name . "<br />";
foreach($value as $a => $price) {
echo $a .': '. $price[0].'-'.$price[1];
}
echo "<br /><br />";
}
The code to achieve what you want would be following, but i suggest you brush up on your PHP skills a bit more, as this is a trivial task.
foreach($suppliers as $name => $data){
echo $name . '<br/>';
foreach($data as $utility => $value){
echo $utility . ': ' . $value[0] . '-' . $value[1] . ' ';
}
echo '<br/><br/>';
}
Same answer as everyone else (I'm too slow). Here's a working example: http://codepad.org/PDPEjAGJ
Also, everyone who answered this question, me included, is guilty of spoonfeeding. Ahh well, the things I'll do for points! :p
Here is a simple recursive function one can use. I found it and modified it for presentation purposes. The original source is in the comments.
function print_a($array, $level=0){
# source: https://thisinterestsme.com/php-using-recursion-print-values-multidimensional-array/
foreach($array as $key => $value){
# If $value is an array.
if(is_array($value)){
echo str_repeat("-", $level). "<br>{$key}<br>\r\n";
# We need to loop through it.
print_a($value, $level + 1);
} else{
# It is not an array, so print it out.
echo str_repeat("-", $level) . "{$key}: {$value}<br>\r\n";
}
}
} # END FUNCTION print_a

PHP post checkboxes help

I"m having trouble receiving a list of items that are checked in a field of checkboxes which are part of a form.
I have an HTML table with a few checkboxes:
HTML
<input type="checkbox" name="carry[]" value="1" />
<input type="checkbox" name="carry[]" value="2" />
<input type="checkbox" name="carry[]" value="3" />
PHP - this is what I'm using to post the form to an email address
foreach($_POST as $key => $val) {
$body .= $key . " : " . $val . "\r\n";
I get the value in my email as: "carry: Array" -- not the actual values that are selected. How do I handle an array of checkboxes selected in a form and post it?
Ideally, I would want: "carry: 1; 2; 3" (without the quotes)
You can check if the value is an array and handle it differently:
foreach($_POST as $key => $val) {
if (is_array($val)) {
$body .= $key . " : " . implode(",",$val) . "\r\n";
} else {
$body .= $key . " : " . $val . "\r\n";
}
}
If you want the string '1; 2; 3', you should join together the items in the array:
$carry= implode('; ', $_POST['carry']);
However doing so will naturally cause ambiguous results if any of the items in the array themselves have a semicolon in.
To iterate over the post array allowing any of its members to be arrays:
foreach($_POST as $key=>$val) {
if (is_array($val))
$val= implode('; ', $val);
$body.= "$key: $val\r\n";
}
Or, if you don't need so much control over the exact formatting and a debugging dump is fine (but you need to see more than just the useless string 'Array':
$body= var_export($_POST, TRUE);
It's printing carry: Array because that's exactly what it is. You need to loop over it (another loop inside the first) to access the values inside:
foreach($_POST as $key => $val) {
if($key == 'carry') {
foreach($val as $carry) {
$body .= $carry;
}
}
else {
$body .= $key . " : " . $val . "\r\n";
}
}
That's completely untested but hopefully the logic is sound :)

Categories