473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Weird $_POST behavior when trying to unset elements

[PHP]
// NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF
FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA
$_POST
if ($_POST['hasUpdatedLease'] && (!is_array($leaseObj->errorArray) ||
@sizeof($leaseObj->errorArray) == 0)) {
print_r(array_keys($_POST));
@reset($_POST);
$tempPost = $_POST;
foreach ($_POST as $key =$val) if (strpos($key, 'new_') === 0)
array_remove($tempPost, $tempPost[$key]);
@reset($_POST);
$_POST =& $tempPost;
print_r(array_keys($_POST));
}
[/PHP]

For some reason, even though the condition is always met when I click
any of the form submit images, the following "weird behavior patterns"
occur:

1) If I click the "Add" submit image, all $_POST values whose keys
start with "new_" should be removed from $_POST. However,
$_POST['new_audited'] and $_POST['new_reoccuring'] remain

2) If I click the "Delete" submit image, all $_POST values whose keys
start with "new_" should be removed from $_POST. However, *ALL* of
them remain in $_POST untouched!

I can verify that I enter the conditional block by my arrays appearing
on screen every time.

Any reason why this is happening that I missed?

Thanx
Phil

Nov 28 '06 #1
5 2276
"comp.lang.php" <ph**************@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
[PHP]
// NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF
FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA
$_POST
if ($_POST['hasUpdatedLease'] && (!is_array($leaseObj->errorArray) ||
@sizeof($leaseObj->errorArray) == 0)) {
print_r(array_keys($_POST));
@reset($_POST);
$tempPost = $_POST;
foreach ($_POST as $key =$val) if (strpos($key, 'new_') === 0)
array_remove($tempPost, $tempPost[$key]);
@reset($_POST);
$_POST =& $tempPost;
print_r(array_keys($_POST));
}
[/PHP]

For some reason, even though the condition is always met when I click
any of the form submit images, the following "weird behavior patterns"
occur:

1) If I click the "Add" submit image, all $_POST values whose keys
start with "new_" should be removed from $_POST. However,
$_POST['new_audited'] and $_POST['new_reoccuring'] remain

2) If I click the "Delete" submit image, all $_POST values whose keys
start with "new_" should be removed from $_POST. However, *ALL* of
them remain in $_POST untouched!

I can verify that I enter the conditional block by my arrays appearing
on screen every time.

Any reason why this is happening that I missed?


Never heard of array_remove, I wonder what it does. Could it be it's not
actually removing anything? Why not just:
unset($tempPost[$key]);

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Nov 28 '06 #2

Kimmo Laine wrote:
"comp.lang.php" <ph**************@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
[PHP]
// NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF
FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA
$_POST
if ($_POST['hasUpdatedLease'] && (!is_array($leaseObj->errorArray) ||
@sizeof($leaseObj->errorArray) == 0)) {
print_r(array_keys($_POST));
@reset($_POST);
$tempPost = $_POST;
foreach ($_POST as $key =$val) if (strpos($key, 'new_') === 0)
array_remove($tempPost, $tempPost[$key]);
@reset($_POST);
$_POST =& $tempPost;
print_r(array_keys($_POST));
}
[/PHP]

For some reason, even though the condition is always met when I click
any of the form submit images, the following "weird behavior patterns"
occur:

1) If I click the "Add" submit image, all $_POST values whose keys
start with "new_" should be removed from $_POST. However,
$_POST['new_audited'] and $_POST['new_reoccuring'] remain

2) If I click the "Delete" submit image, all $_POST values whose keys
start with "new_" should be removed from $_POST. However, *ALL* of
them remain in $_POST untouched!

I can verify that I enter the conditional block by my arrays appearing
on screen every time.

Any reason why this is happening that I missed?

Never heard of array_remove, I wonder what it does. Could it be it's not
actually removing anything? Why not just:
unset($tempPost[$key]);
oh sorry I forgot to include the function:

if (!function_exists('array_remove')) { // FUTURISTIC: IN CASE AN
"array_remove" PHP FUNCTION IS MADE PART OF CORE IN THE FUTURE
/**
* Remove a specific element from the array. If not found return the
array as-is
*
* @access public
* @param array $array (reference)
* @param mixed $element
*/
function array_remove(&$array, $element) {
if ($element && @in_array($element, $array))
unset($array[array_search($element, $array)]);
}
}
>
--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Nov 28 '06 #3
"comp.lang.php" <ph**************@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
Kimmo Laine wrote:

Never heard of array_remove, I wonder what it does. Could it be it's not
actually removing anything? Why not just:
unset($tempPost[$key]);

oh sorry I forgot to include the function:

if (!function_exists('array_remove')) { // FUTURISTIC: IN CASE AN
"array_remove" PHP FUNCTION IS MADE PART OF CORE IN THE FUTURE
/**
* Remove a specific element from the array. If not found return the
array as-is
*
* @access public
* @param array $array (reference)
* @param mixed $element
*/
function array_remove(&$array, $element) {
if ($element && @in_array($element, $array))
unset($array[array_search($element, $array)]);
}
}

Still, why not just unset the element right away. Now you're risking of
removing the wrong element when you remove something by the value, not key.
Imagine a case like this:
$foo = array('0','0','0','0','0');

If you tell me to remove the element that has the value '0', how am I gonna
know which element it is, if there are five zeros? Instead, if you tell me
to remove the fourth element, I have no trouble telling which element you
mean. I think the real problem here is actually that wrong elements are
removed from the array. because they ahve the same value. It says in the
manual: "If $needle is found in $haystack more than once, the first matching
key is returned."

Just try this code and see what happens:
$foo = array('0','0','0','0','0');
print_r($foo);
array_remove($foo, $foo[4]);
print_r($foo); // Was the fourth element removed? I think not.

And then try this:
$foo = array('0','0','0','0','0');
print_r($foo);
unset($foo[4]);
print_r($foo); // *Now* was the fourth element removed?
--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Nov 28 '06 #4

Kimmo Laine wrote:
"comp.lang.php" <ph**************@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
Kimmo Laine wrote:
>
Never heard of array_remove, I wonder what it does. Could it be it's not
actually removing anything? Why not just:
unset($tempPost[$key]);
oh sorry I forgot to include the function:

if (!function_exists('array_remove')) { // FUTURISTIC: IN CASE AN
"array_remove" PHP FUNCTION IS MADE PART OF CORE IN THE FUTURE
/**
* Remove a specific element from the array. If not found return the
array as-is
*
* @access public
* @param array $array (reference)
* @param mixed $element
*/
function array_remove(&$array, $element) {
if ($element && @in_array($element, $array))
unset($array[array_search($element, $array)]);
}
}


Still, why not just unset the element right away. Now you're risking of
removing the wrong element when you remove something by the value, not key.
Imagine a case like this:
$foo = array('0','0','0','0','0');

Then I would use array_remove_element_at() instead:

if (!function_exists('array_remove_element_at')) { // FUTURISTIC: MODEL
AFTER JAVA Vector.removeElementAt(index)
/**
* Function modeled after {@link
http://java.sun.com/j2se/1.4.2/docs/...eElementAt(int)
java.util.Vector.removeElementAt((integer)index)}
*
* Unlike Java, in PHP you will remove element at array index and
return it
*
* @access public
* @param array $array (reference)
* @param mixed $key
* @return mixed $element
*/
function array_remove_element_at(&$array, $key = '') {
if (is_numeric($key) || (!is_numeric($key) && $array[$key])) {
if (is_numeric($key)) {
$element = $array[$key];
unset($array[$key]);
$array = @array_values($array); // RE-ORDER ENUMERATIVE ARRAY
} elseif (!is_numeric($key) && $array[$key]) {
$element = $array[$key];
unset($array[$key]);
}
}
return $element;
}
}

Phil
>
If you tell me to remove the element that has the value '0', how am I gonna
know which element it is, if there are five zeros? Instead, if you tell me
to remove the fourth element, I have no trouble telling which element you
mean. I think the real problem here is actually that wrong elements are
removed from the array. because they ahve the same value. It says in the
manual: "If $needle is found in $haystack more than once, the first matching
key is returned."

Just try this code and see what happens:
$foo = array('0','0','0','0','0');
print_r($foo);
array_remove($foo, $foo[4]);
print_r($foo); // Was the fourth element removed? I think not.

And then try this:
$foo = array('0','0','0','0','0');
print_r($foo);
unset($foo[4]);
print_r($foo); // *Now* was the fourth element removed?
--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Nov 28 '06 #5

comp.lang.php wrote:
Kimmo Laine wrote:
"comp.lang.php" <ph**************@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
Kimmo Laine wrote:

Never heard of array_remove, I wonder what it does. Could it be it's not
actually removing anything? Why not just:
unset($tempPost[$key]);
>
oh sorry I forgot to include the function:
>
if (!function_exists('array_remove')) { // FUTURISTIC: IN CASE AN
"array_remove" PHP FUNCTION IS MADE PART OF CORE IN THE FUTURE
/**
* Remove a specific element from the array. If not found return the
array as-is
*
* @access public
* @param array $array (reference)
* @param mixed $element
*/
function array_remove(&$array, $element) {
if ($element && @in_array($element, $array))
unset($array[array_search($element, $array)]);
}
}

Still, why not just unset the element right away. Now you're risking of
removing the wrong element when you remove something by the value, not key.
Imagine a case like this:
$foo = array('0','0','0','0','0');


Then I would use array_remove_element_at() instead:

if (!function_exists('array_remove_element_at')) { // FUTURISTIC: MODEL
AFTER JAVA Vector.removeElementAt(index)
/**
* Function modeled after {@link
http://java.sun.com/j2se/1.4.2/docs/...eElementAt(int)
java.util.Vector.removeElementAt((integer)index)}
*
* Unlike Java, in PHP you will remove element at array index and
return it
*
* @access public
* @param array $array (reference)
* @param mixed $key
* @return mixed $element
*/
function array_remove_element_at(&$array, $key = '') {
if (is_numeric($key) || (!is_numeric($key) && $array[$key])) {
if (is_numeric($key)) {
$element = $array[$key];
unset($array[$key]);
$array = @array_values($array); // RE-ORDER ENUMERATIVE ARRAY
} elseif (!is_numeric($key) && $array[$key]) {
$element = $array[$key];
unset($array[$key]);
}
}
return $element;
}
}

Phil

If you tell me to remove the element that has the value '0', how am I gonna
know which element it is, if there are five zeros? Instead, if you tellme
to remove the fourth element, I have no trouble telling which element you
mean. I think the real problem here is actually that wrong elements are
removed from the array. because they ahve the same value. It says in the
manual: "If $needle is found in $haystack more than once, the first matching
key is returned."

Just try this code and see what happens:
$foo = array('0','0','0','0','0');
print_r($foo);
array_remove($foo, $foo[4]);
print_r($foo); // Was the fourth element removed? I think not.

And then try this:
$foo = array('0','0','0','0','0');
print_r($foo);
unset($foo[4]);
print_r($foo); // *Now* was the fourth element removed?
FIXED!

Got it, apparently my own blunder; I was using the wrong user-defined
function: instead of array_remove(), I needed
array_remove_element_at():

if (!function_exists('array_remove_element_at')) { // FUTURISTIC: MODEL
AFTER JAVA Vector.removeElementAt(index)
/**
* Function modeled after {@link
http://java.sun.com/j2se/1.4.2/docs/...eElementAt(int)
java.util.Vector.removeElementAt((integer)index)}
*
* Unlike Java, in PHP you will remove element at array index and
return it
*
* @access public
* @param array $array (reference)
* @param mixed $key
* @return mixed $element
*/
function array_remove_element_at(&$array, $key = '') {
if (is_numeric($key) || (!is_numeric($key) && $array[$key])) {
if (is_numeric($key)) {
$element = $array[$key];
unset($array[$key]);
$array = @array_values($array); // RE-ORDER ENUMERATIVE ARRAY
} elseif (!is_numeric($key) && $array[$key]) {
$element = $array[$key];
unset($array[$key]);
}
}
return $element;
}
}
$tempPost = $_POST;
foreach ($_POST as $key =$val) if (strpos(trim($key), 'new_') ===0)
@array_remove_element_at($tempPost, $key);
$_POST =& $tempPost;

Also obviously don't need to use reset() either since the array pointer
is irrelevant in this case, at least I think so..

Phil

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Nov 28 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Raptor | last post by:
I'm using a single script to generate a table with <input>s in each row. I fill the array with initial values, then write it out to the table and let the user edit the values. Something like: ...
1
by: Jenkins | last post by:
I've loaded various thoughts I've collected into mysql. I want to write an app, all in one file, that has the following features: 1. On initial entry, selects and displays the thoughts ( might...
14
by: Sergei Riaguzov | last post by:
I have a very strange behaviour with POST keys. Consider a test: wtf.html: === Cut === <form action="eh.php" method="POST">     <input type="checkbox" name="many spaces and. . dots. . "/>...
3
by: Chuck Renner | last post by:
Please help! This MIGHT even be a bug in PHP! I'll provide version numbers and site specific information (browser, OS, and kernel versions) if others cannot reproduce this problem. I'm...
5
Tarantulus
by: Tarantulus | last post by:
Hi, ok, quick description of the problem, I'm trying to reference the postdata from some checkboxes. Unfortunately the checkboxes are dynamically generated therefore I don't know how many there...
4
by: mtuller | last post by:
I have a page that submits data to a database, and I want to make it so that if the page is refreshed, it doesn't submit the information again. I am trying to use unset the variables so that if the...
10
by: Mason Barge | last post by:
I have a standard POST form consisting of two types of input: text input and textarea. The form downloads current settings from a mysql database. The user can update the information by modifying...
0
by: Gilles Ganault | last post by:
Hello I use a POST variable named "status" to show different screens while keeping everything in the same script. The problem is that in the Print screen, I need to empty this variable so the...
22
by: William Gill | last post by:
This may sound stupid but, in my testing I have noticed that I get every form field ($_POST array key), even if not populated. I am sure that's not how things worked before, but the point is, is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.