On Mon, Feb 8, 2016 at 12:53 PM, Larry Garfield <la...@garfieldtech.com>
wrote:

> On 2/8/16 11:46 AM, Kinn Julião wrote:
>
>> On Mon, Feb 8, 2016 at 11:38 AM, Rowan Collins <rowan.coll...@gmail.com>
>> wrote:
>>
>> Kinn Julião wrote on 08/02/2016 16:05:
>>>
>>> That's the nice thing about userland... you are giving them ability to
>>>> implement what they want!
>>>> `array_filter([...], 'is_int', ARRAY_FILTER_USE_KEY)` does the job as
>>>> the
>>>> others... don't think having a new function would prevent this.
>>>>
>>>> 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.
>>>
>>> How does `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.
>>
>
> 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"?
>

It doesn't really matter, and the explanation is below[1] (you actually
wrote it).


>
> If the latter, $foo = array_values($bar); is a reasonably efficient and
> reasonably bulletproof way to ensure you have a strictly numeric array.
>
> Agreed.


> PHP 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 have `Map<int, mixed>` than `has_numeric_keys`
>>
>
> 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.)
>
> [1] - That's why it doesn't matter if it's to restrict or validate...

As the top post mentions:

`
function isArrNum($arr) {
  foreach($arr as $i =>$v){
    if (!is_int($i)) {
      return false;
    }
  }
  return true;
}
`

If you want to check the keys type.

```
return $variable instanceof Map;
```

Don't want to bc?
```
function isArrNum($arr)
  if (class_exists('Map')) $variable instanceof Map;

  foreach($arr as $i =>$v){
    if (!is_int($i)) {
      return false;
    }
  }
  return true;
}
```

Ugly? sure... but at least now we have support to a better structure...
remember: Working towards making PHP 7 even better... adding support to
Map(or whatever people wanna call it) can make it better, adding a
`has_numeric_keys`, IMHO, is just an `alias` to other userland options.

> --
> --Larry Garfield
>
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
*--*
*Kinn Coelho Julião*
*Toronto - ON/Canada*

Reply via email to