By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,844 Members | 836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,844 IT Pros & Developers. It's quick & easy.

Weird $_POST behavior when trying to unset elements

P: n/a
[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
Share this Question
Share on Google+
5 Replies


P: n/a
"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

P: n/a

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

P: n/a
"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

P: n/a

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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.