rfc:array_part
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
Previous revisionNext revision | |||
— | rfc:array_part [2012/05/28 15:19] – [Specification for the function] cataphract | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Request for Comments: array_part ====== | ||
+ | * Version: 1.1 | ||
+ | * Date: 2012-05-14 | ||
+ | * Author: Gustavo Lopes < | ||
+ | * Status: Under Discussion | ||
+ | * First Published at: http:// | ||
+ | |||
+ | |||
+ | Introduces a new function '' | ||
+ | |||
+ | ===== Introduction ===== | ||
+ | |||
+ | This RFC proposes a new array function that can extract multidimensional slices for arrays. | ||
+ | |||
+ | ===== Specification for the function ===== | ||
+ | |||
+ | The function '' | ||
+ | |||
+ | array array_part(array $originalArray, | ||
+ | |||
+ | The parameter '' | ||
+ | |||
+ | The parameter '' | ||
+ | |||
+ | Each part specification shall have one of the following forms: | ||
+ | |||
+ | * A non-empty sequential numeric array of indexes, which specifies that only the elements existing at those indexes will be kept. | ||
+ | * A single index, which specifies that only the element existing at that index will be kept. In this case the level will be collapsed onto the previous one, meaning all the arrays at that level will be replaced with its element the specified index. | ||
+ | * A span part specification is an associative array. The following keys are allowed //start//, //end// and //step//. At least //start// or //end// must be specified. //start// and //end// are an index or a special value -- '' | ||
+ | |||
+ | Span parts extract any number of elements (possibly 0) starting at the index specified by //start// and advancing until //end// is reached, advancing in steps of //step//. The element at the index specified at //end// is included. | ||
+ | |||
+ | |||
+ | An index //i// has the following meaning: | ||
+ | * If '' | ||
+ | * If '' | ||
+ | * If '' | ||
+ | |||
+ | The keys in the original array are not preserved in the output, expect at levels that were not visited. References are not preserved when the elements are added to the resulting array. | ||
+ | |||
+ | This function shall return '' | ||
+ | |||
+ | * Giving arguments with different PHP types from those specified. | ||
+ | * Giving malformed part specifications (where the individual elements do not follow the syntactic rules given here). | ||
+ | * Specifying an part index than does not exist in at least one of the elements at the level the part refers to: (e.g. '' | ||
+ | * Giving a part specification with levels that do not exist in the input. This does not apply if, as a result of a span part that comprises all the elements of that level and at the level before existed only empty arrays, no elements were left for the next level. For instance ' | ||
+ | |||
+ | ===== Proposed implementation ===== | ||
+ | |||
+ | The proposed implementation is available as [[https:// | ||
+ | |||
+ | ===== Sample PHP implementation and usage ===== | ||
+ | |||
+ | A sample implementation, | ||
+ | |||
+ | This implementation differs in some respects to the internal implementation with respect to behavior. Its purpose is only to exemplify the usage of the function here proposed. | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | ===== Comments ===== | ||
+ | I would find this far more useful if the keys were preserved, or at least an option to do so. | ||
+ | Or the old standby that INT keys get renumbered but non-INT do not. | ||
+ | |||
+ | |||
+ | ===== Objections ===== | ||
+ | |||
+ | **I want native array slicing through a new operator** | ||
+ | |||
+ | That's fine, but it is not what this proposal is about. A change to the language presents many more complications which I very much want to avoid -- for instance the current array dereferencing syntax most likely would unusable because -1 represents the element with key -1, not the last element. Besides, the introduction of this function does not prevent native array slicing from being added in the future. | ||
+ | |||
+ | **You seem to already have an implementation, | ||
+ | |||
+ | First, this function would benefit greatly from a native implementation. It's impossible to efficiently detect recursion in userland. The sample makes heavy use of references, which introduces a lot of separations. Traversing arrays without changing the internal pointer is very inefficient in userland. | ||
+ | |||
+ | Of course, the main reason is that this function is useful. Recently, functions like '' | ||
+ | |||
+ | **The $indexesAreKeys = false mode is inefficient because there' | ||
+ | |||
+ | This is true. We must traverse the array from the start or the end to get to the n-th element. That's just the way PHP arrays are implemented. However, if you have numeric sequential arrays, you can use '' | ||
+ | |||
+ | ===== Vote ===== | ||
+ | |||
+ | < | ||
+ | title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ===== Changelog ===== | ||
+ | |||
+ | * 2012-05-14 Initial version | ||
+ | * 2012-05-21 Dropped recursion restriction, | ||
+ | * 2012-05-21 References are not preserved after all | ||
+ | * 2012-05-28 Vote opened | ||
rfc/array_part.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1