Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91126 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 81972 invoked from network); 8 Feb 2016 17:44:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Feb 2016 17:44:00 -0000 Authentication-Results: pb1.pair.com header.from=me@mprelu.de; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=me@mprelu.de; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mprelu.de from 74.201.84.163 cause and error) X-PHP-List-Original-Sender: me@mprelu.de X-Host-Fingerprint: 74.201.84.163 sender163-mail.zoho.com Received: from [74.201.84.163] ([74.201.84.163:25529] helo=sender163-mail.zoho.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EC/07-36326-FD3D8B65 for ; Mon, 08 Feb 2016 12:44:00 -0500 Received: from [192.168.0.5] (bcdfe029.skybroadband.com [188.223.224.41]) by mx.zohomail.com with SMTPS id 1454953436445117.36276702998634; Mon, 8 Feb 2016 09:43:56 -0800 (PST) To: Matthew Setter References: Cc: internals@lists.php.net Message-ID: <56B8D3D9.3040904@mprelu.de> Date: Mon, 8 Feb 2016 17:43:53 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Zoho-Virus-Status: 2 Subject: Re: [PHP-DEV] Proposal for a new array function From: me@mprelu.de (Matt Prelude) Hi, > I want to propose a new PHP array method, called has_numeric_keys (or > something similar/better), that would have the following method signature: > > bool has_numeric_keys(array $array) > > The reason for it is to check if the array passed to it only had numeric > keys. > > Why this method, when you could do it in userland PHP? Answer? Convenience. > I found, recently, that I had to perform this kind of check, when patching > Zend\Db\Sql\Insert.php. The approach I took was this: > > return array_keys($arr) !== range(0, count($arr) - 1); > > Not sure of my approach, I took to Twitter and received the following > suggestions, amongst others: > > function isArrNum($arr) { > foreach($arr as $i =>$v){ > if (!is_int($i)) { > return false; > } > } > return true; > } > > count(array_filter(array_keys($array), 'is_string')) > 0 > > array_filter([...], 'is_int', ARRAY_FILTER_USE_KEY) > > This convinced me that it wasn't just me seeing a valid use case for it, > and that others have implemented differing solutions when presented with > the same situation. Given that, I believe a simple, utility, function such > as this would be of help. > > As for who would implement it, that would be me. I like it. Would like to see it be a bit more generic though, something like (ignore the name, I can't name functions): array_validate_keys($array, is_int); I say this because it allows for more potential uses (that and I've done exactly the same to check all keys are strings before). - Matt.