Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91140 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41004 invoked from network); 9 Feb 2016 09:01:49 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Feb 2016 09:01:49 -0000 X-Host-Fingerprint: 89.236.29.179 h59ec1db3.selucle.dyn.perspektivbredband.net Received: from [89.236.29.179] ([89.236.29.179:10405] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 22/20-39202-BFAA9B65 for ; Tue, 09 Feb 2016 04:01:47 -0500 Message-ID: <22.20.39202.BFAA9B65@pb1.pair.com> To: internals@lists.php.net References: Date: Tue, 9 Feb 2016 10:01:42 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Posted-By: 89.236.29.179 Subject: Re: Proposal for a new array function From: sisve@devhost.se (Simon Svensson) On 08/02/16 15:32, Matthew Setter wrote: > 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. > > -- > Kind regards, > > > *Matthew Setter* > *Freelance Software Developer & Technical Writer * > *Host of Free the Geek | Author of Zend Framework 2 > Foundations * > > w: http://www.matthewsetter.com > t: *@settermjd * > g+: *+MatthewSetterFreelanceWriterDeveloper > * > li: *in/matthewsetter * > Hi, Note that your approach ("return array_keys($arr) !== range(0, count($arr) - 1);") does another type of check than the name has_numeric_keys implies. Your approach will also validate that the keys are continuous. This matches a check for non-associative arrays. Is that the goal with this function, to help developers determine whether an array is associative or not? Are you actually asking for a array_is_assoc($arr) function? // Simon