How do I exclude hidden field from PHP foreach loop - php

I have a simple form that inserts data into a database using foreach($_POST as $key=>$value) I have a hidden field on the form
<input name="isset" type="hidden" value="true" />
And i use if(isset($_POST['isset'])) {
I'm trying to work out how to exclude the hidden field from the loop ...?
I've looked at this post but don't understand where i would use if (strpos($key, 'hdn_') == false) // proceed
How to exclude <input type="hidden"> from a for each loop in PHP
any guidance would be appreciated....

If you know the exact names of keys you want to exclude, array_diff_key is a convenient option:
$keysToRemove = array('isset'); // you can add as many as you want
$values = array_diff_key($_POST, array_flip($keysToRemove));
foreach ($values as $k => $v) { ... }
However, since $values is intended to go into the database you should use a whitelist of allowed keys instead of a blacklist. You can do that with array_intersect_key:
$keysToKeep = array('field1', 'field2', 'field3'); // as many as you want
$values = array_intersect_key($_POST, array_flip($keysToKeep));
foreach ($values as $k => $v) { ... }

Inside the foreach:
foreach ($_POST as $key => $value) {
if ($key != 'isset') {
//code here
}
}
(For what I got from your question)
Or from your array, you can unset() the element with the 'isset' array key.

Related

loop for part of a multidimensional array

I've done a multistep form in PHP storing the data in a multidimensional array (I've created an array inside $_SESSION array and named it $_SESSION['inserimento'])
then i have $_SESSION['inserimento']['name'],$_SESSION['inserimento']['city']...
I would like to apply the strtolower() function to all the values before adding them to mysql
I've tried this code but it doesn't work
foreach ($_SESSION['inserimento'] as $k=>$v){
$v=strtolower($v);
}
I think I'm misunderstanding how to make a loop on multidimensional array.
Use array_map() to apply a function to all elements in an array:
$_SESSION['inserimento'] = array_map('strtolower', $_SESSION['inserimento']);
Or a regular foreach loop (inside the loop $v is a copy, so you need to affect to the original array):
foreach ($_SESSION['inserimento'] as $k => $v) {
$_SESSION['inserimento'][$k] = strtolower($v);
}
Or a foreach loop with reference ($v is no longer a copy, it is a reference to the original element):
foreach ($_SESSION['inserimento'] as &$v) {
$v = strtolower($v);
}
unset($v); // remember to unset, or $v will still be a reference to the last element after the loop
Use:
foreach ($_SESSION['inserimento'] as $k => $v) {
$_SESSION['inserimento'][$k] = strtolower($v);
}
This is happening because $v is a copy of the value inside the iteration, not a reference to the variable that contains the value.
You need to have a variable defined outside of the foreach loop.
$lowerValue = '';
foreach ($_SESSION['inserimento'] as $k => $v) {
$lowerValue = strtolower($v);
}
Try using array_walk
array_walk($_SESSION['inserimento'], function(&$value, $key) {
$value = strtolower($value);
});
The & before $value indicates that the variable is passed by reference.

How do I get key name from $key in foreach($_POST as $key=>$value){

I have dynamic field names that are being posted to my form processor. The names are formatted like editnote|9. |9 being the ID of the note to edit. If I use this code:
foreach($_POST as $key) {
$editcheck = explode('|', $key);
if($editcheck[0] == 'editnote') {
echo $editcheck[1];
}
}
I can properly get the ID of 9, but I cannot get the value. I have tried:
foreach($_POST as $key => $value) {
$editcheck = explode('|', $key);
if($editcheck[0] == 'editnote') {
$clsCNA->updatenote($editcheck[1], $_POST[$key]);
}
}
But with this code, I can only get the value and not the key name to get the note ID I want to edit.
So basically, I want foreach($_POST as $key => $value) to somehow allow me to get the name of $key so I can explode it to get the ID number.
Any ideas?
I am trying to send to my class like this:
$clsCNA->updatenote(ID From key name, Value of key)
If I use this code:
foreach($_POST as $key) {
$editcheck = explode('|', $key);
if($editcheck[0] == 'editnote') {
echo $editcheck[1];
}
}
I can properly get the ID of 9...
foreach($_POST as $key => $value) {
CUT ...
As PHP's foreach loop using one variable will only take the values of the array. The exact names chosen does not determine what what goes into the variables. Similarly found by method calls.
This will be put the Value into the $key variable.
In the second example you are using the $key => $value syntax. Here the loop puts the Key into the $key variable and the Value into the $value variable.
What this means is that your $key variable is not the same in both examples. In one it holds the Values of the array in the other it holds the Keys.

php update values of an array based on other array with the same key

I have the following scenario:
$starterArray = array ('192.168.3.41:8013'=>0,'192.168.3.41:8023'=>0,'192.168.3.41:8033'=>0);
In the requirement I have another array which counts some events of the application, this array uses the same keys as my first array, but values can change), so at the end I could have something like:
$processArray = array ('192.168.3.41:8013'=>3,'192.168.3.41:8023'=>5,'192.168.3.41:8033'=>7);
I want to update the values of my starter array with the values of the process array, for instance, at the end, I should have:
$starterArray = array ('192.168.3.41:8013'=>3,'192.168.3.41:8023'=>5,'192.168.3.41:8033'=>7);
I know this can be achieved by using $starterArray = $processArray;
Then in some moments, I would need to sum some units to the values of my array, for example +1 or +2:
It should be something like the following?
foreach ($starterArray as $key => $value) {
$starterArray[$value] = $starterArray[$value]+1;
}
Then, for my process array, I need to set the values to 0
foreach ($processArray as $key => $value) {
$processArray[$value] = 0;
}
This is what I tried but it is not working, if somebody could help me I will really appreaciate it. Thanks in advance.
PD: I know these are strange requirements, but that's what I am asked to do...
You are almost there:-
foreach ($processArray as $key => $value) {
$starterArray[$key] = $value +1;
}
and then:-
foreach ($processArray as $key => $value) {
$processArray[$key] = 0;
}
However, you could do this all in one loop:-
foreach ($processArray as $key => $value) {
$starterArray[$key] = $value +1;
$processArray[$key] = 0;
}
You need to put $key in brackets, not $value.
Or, you can do:
foreach ($starterArray as $key => &$value) {
$value++; /* put here whatever formula you want */
}
foreach ($starterArray as $key => $value) {
$starterArray[$key] = $value+1;
// or $starterArray[$key] = 0;
}

How can I implement scoped foreach in PHP?

I get data from an excel spreadsheet, loop over it, save it to an array. Then, I loop over that data twice.
I do:
foreach($someData as $key => $value) {
}
and I will need to foreach that same array again. Is there any way I can make it so i can use $key => $value again without causing any problems?
It sounds like you only need one loop, but to answer your question, you can just do this:
foreach($someData as $key => $value) {
#do stuff here
}
foreach($someData as $key => $value) {
#do more stuff here
}
$key and $value will get overwritten during each iteration of each loop, so there's no danger here.
Your question is a little unclear, though—if you have a foreach inside of another foreach and want to use the same set of variable names for each loop's keys and values, use a function. That's the only way to create a new local scope in PHP:
function nested_loop($arr) {
foreach($arr as $key => $value) {
#do more stuff here
}
}
foreach($someData as $key => $value) { #same names, different variables
#do stuff here
nested_loop($value);
}

How do I return specific keys from the $_POST array?

I would like to check to see if there are any keys in $_POST that contain a string. The string will not be the full key, only part of the key. (ie. search string = "delRowID", $_POST key = "delRowID_16"). I have tried to use array_keys($_POST,"delRowID"), but it has never returned anything.
CODE
print_r($_POST);
print_r(array_keys($_POST,"delRowID"));
RETURNS
Array ( [delRowID] => 29 [qAction] => [elmUpdateId] => [elmTtl] => [elmDesc] => [elmStr] => ) Array ( )
If this is being sent by a form, considering naming the elements as array elements. For example,
<input type="checkbox" name="delRowID[16]" />
<input type="checkbox" name="delRowID[17]" />
would come in as an array named $_POST['delRowID'] with elements for each valid input.
However, this is a contrived example which works better with other input types.
For checkboxes, it would be better done like this, which creates an array with a value for each successful checkbox that you can easily loop over:
<input type="checkbox" name="delRowID[]" value="16" />
<input type="checkbox" name="delRowID[]" value="17" />
See Also: How do I create arrays in a HTML <form>?
Do a loop using array_keys() and check the key with strpos()
foreach (array_keys($_POST) as $key) {
if (strpos($key, 'delRowId') === 0) {
echo $key." found!";
}
}
Loop through the keys provided to you by array_keys($_POST). Do a string match on each.
Also, note that array_keys($_POST,"delRowID") searches for keys that are associated with a value of "delRowID".
Because you are searching for partial text, you can loop through it:
foreach($_POST as $key => $value)
{
if (strpos($key, 'delRowID') !== false)
{
echo $key;
break;
}
}
Just another way (extending mads answer):
if( getKey( 'delRowId', $_POST ) ){
// delRow?
}
function getKey($stringToFind, $array) {
foreach ($_POST as $key => $val) {
if (strpos($stringToFind, $key) !== false) {
return $val;
}
}
return false;
}

Categories