Re: Proposal for a new array function
| From: | Larry Garfield | Date: | Mon, 08 Feb 2016 17:53:45 +0000 |
| Subject: | Re: Proposal for a new array function | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-91129@lists.php.net to get a copy of this message | ||
On 2/8/16 11:46 AM, Kinn Julião wrote:
On Mon, Feb 8, 2016 at 11:38 AM, Rowan Collins <rowan.collins@gmail.com> wrote:I'm confused at the goal here. Is the intent "I need to do different logic if this is a numeric array vs. something else" or "I need to make sure this is a numeric array, because things break otherwise"? If the latter, $foo = array_values($bar); is a reasonably efficient and reasonably bulletproof way to ensure you have a strictly numeric array.Kinn Julião wrote on 08/02/2016 16:05:How doesThat's the nice thing about userland... you are giving them ability to implement what they want!It does the job, but it is much less efficient in some cases than the foreach and return false case, as it must examine every element; it also has unnecessary memory overhead, creating a filtered list which exists only to be counted and thrown away; and an internal implementation could be optimised even further by exploiting the way the engine stores the array internally.array_filter([...], 'is_int', ARRAY_FILTER_USE_KEY)does the job as the others... don't think having a new function would prevent this.array_filter([...], 'is_int', ARRAY_FILTER_USE_KEY)add that *much* overhead? And if that's the case, instead of introducing a 99999 array function into the core, why not just create a new Object then? $list = list('foo','bar'); // I know it's a reserved token, don't have time to find a better synonym. it's just a sample $list[2] = 'baz'; var_dump($list); string(1) "foo" string(1) "bar" string(1) "baz" $list['a'] = 'foobar'; // Fatal error.
Or that. Which I like even better. Ahead-of-time typing is much safer at scale than runtime typing. (Of course we then run smack into the arrays-and-objects-are-just-different-enough-to-be-annoying problem that has plagued us for a decade.) -- --Larry GarfieldPHP is at this stage expressive enough that a large number of core functions could be rewritten in multiple ways using other core functions, but that doesn't automatically rule out adding more functions to core where it is useful to do so.PHP is at a stage where we can add support to a better structure and objects support... we don't need a function to check if the keys are integer, we need an object that only supports integer as keys, or even a generic key... Would rather haveMap<int, mixed>thanhas_numeric_keys