You are here: Re: Weird $_POST behavior when trying to unset elements « PHP Programming Language « IT news, forums, messages
Re: Weird $_POST behavior when trying to unset elements

Posted by comp.lang.php on 11/28/06 16:49

comp.lang.php wrote:
> Kimmo Laine wrote:
> > "comp.lang.php" <phillip.s.powell@gmail.com> wrote in message
> > news:1164702698.875321.253830@l12g2000cwl.googlegroups.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/api/java/util/Vector.html#removeElementAt(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?
> >

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/api/java/util/Vector.html#removeElementAt(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
> > spam@outolempi.net | rot13(xvzzb@bhgbyrzcv.arg)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация