rfc:array_delete

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:array_delete [2012/08/22 02:07] – [array_delete] levimrfc:array_delete [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 3: Line 3:
   * Date: 2012-08-21   * Date: 2012-08-21
   * Author:  Rasmus Schultz <rasmus@mindplay.dk>, Yasuo Ohgaki <yohgaki@ohgaki.net>/<yohgaki@php.net>   * Author:  Rasmus Schultz <rasmus@mindplay.dk>, Yasuo Ohgaki <yohgaki@ohgaki.net>/<yohgaki@php.net>
-  * Status: Under Discussion+  * Status: Inactive
   * First Published at: http://wiki.php.net/rfc/array_delete   * First Published at: http://wiki.php.net/rfc/array_delete
 ===== Introduction ===== ===== Introduction =====
Line 47: Line 47:
  
 To prevent accidental deletion, $strict defaults to true - note that this differs from array_search() where the most likely objective is to "find something". To prevent accidental deletion, $strict defaults to true - note that this differs from array_search() where the most likely objective is to "find something".
 +
 +Above example is inefficient, old PHP users should use array_walk().
 +
 +<code php>
 +function array_delete(&$array, $value, $strict = TRUE) {
 +    $count = 0;
 +    array_walk($array, function($item, $key) use (&$array, &$count, $value, $strict) {
 +        if ($strict) {
 +            if ($item === $value) {
 +                $count++;
 +                unset($array[$key]);
 +            }
 +        } else {
 +            if ($item == $value) {
 +                $count++;
 +                unset($array[$key]);
 +            }
 +        }
 +    };
 +    return $count;    
 +}
 +</code>
 +
 +Note for non internal programmers: array_walk() is not fool safe function. It will cause unwanted behavior if one reorder elements or delete elements to be processed next. Above example works as expected. i.e. Deleting currently working element is normal operation with zend hash.
  
 <code php>int array_delete_recursive(&$array, $value, $strict = TRUE)</code> <code php>int array_delete_recursive(&$array, $value, $strict = TRUE)</code>
Line 112: Line 136:
  
 If user would like to delete elements, they should use array_walk() rather than array_filter() as it does not delete elements, but creates new array. i.e. Memory and execution is inefficient. Stack overflow and internals thread shows users are not able to choose right API for element deletion. Therefore, array_udelete() is worth to have. If user would like to delete elements, they should use array_walk() rather than array_filter() as it does not delete elements, but creates new array. i.e. Memory and execution is inefficient. Stack overflow and internals thread shows users are not able to choose right API for element deletion. Therefore, array_udelete() is worth to have.
 +
 +=== Using array_walk to delete elements is undefined behavior ===
 +
 +It says so [[http://php.net/manual/en/function.array-walk.php#refsect1-function.array-walk-parameters | right in the manual]] (look under funcname's description). There is not currently a defined function that would allow you to delete elements from the array like this, hence the array_delete proposal.
 +
 +=== Please add this ===
 +
 +I was needing something exactly like this!
 +
 +== Deleting CURRENTLY working element is OK ==
 +
 +If you are module or core programmer, you should know deleting CURRENTLY working element will not cause any problems as it is NORMAL operation for zend hash. Please ask if you don't understand what we are talking about before edit RFC.
 +
 +If one reorder element or delete next element to be processed, it will case unwanted behavior. PHP is programming language so shooting your own foot is free for users.
 +
 +However, this brings up good reason to introduce array_udelete(). array_walk() is certainly not a fool safe function while array_udelete() is.
  
 ===== Proposal and Patch ===== ===== Proposal and Patch =====
Line 129: Line 169:
   * 0.4 Reverted callable changes.   * 0.4 Reverted callable changes.
   * 0.5 Add array_udelete()   * 0.5 Add array_udelete()
 +  * 0.6 Removed anything with `array_walk`.  Removing things with array_walk is undefined. Stop putting it back in, please!
rfc/array_delete.1345601272.txt.gz · Last modified: 2017/09/22 13:28 (external edit)