Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91124 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 77578 invoked from network); 8 Feb 2016 17:06:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Feb 2016 17:06:23 -0000 Authentication-Results: pb1.pair.com smtp.mail=cesar@rodas.me; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=cesar@rodas.me; sender-id=pass Received-SPF: pass (pb1.pair.com: domain rodas.me designates 109.74.200.116 as permitted sender) X-PHP-List-Original-Sender: cesar@rodas.me X-Host-Fingerprint: 109.74.200.116 li147-116.members.linode.com Received: from [109.74.200.116] ([109.74.200.116:43111] helo=li147-116.members.linode.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 63/46-36326-E0BC8B65 for ; Mon, 08 Feb 2016 12:06:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rodas.me; s=mail; t=1454951179; bh=bE8UoEdb3sMH7/reUfI2H9OcS+2FeO7FtQUV5UxYggE=; h=To:References:From:Date:In-Reply-To:From; b=aqcHqkyGI2xwoHc2lPdXo/2M7Sc79410qvXez9GeDRDxcJualpmIFLTjeSSjSTSP+ a+/72VIAWrMWCTXMMl+PtTDsemlic6fM2elM1l0xtMYbCm0DYNlQrWOkmWLXre2RKU Rw8DA6CCWcnI6xHrmbBylQb9IYX9/xZlseDGuL3k= To: internals@lists.php.net References: Message-ID: <56B8CB06.6020603@rodas.me> Date: Mon, 8 Feb 2016 14:06:14 -0300 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Proposal for a new array function From: cesar@rodas.me (Cesar Rodas) On 2/8/16 2:04 PM, Eugene wrote: > On Mon, Feb 8, 2016 at 3:32 PM, 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. `array_keys($array) === range(0, count($array)-1)` This is how I check, and it seems a bit faster. >> >> 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 * >> > > > Usually, checking only numeric keys is not enough. You may also need to > check if those numeric keys are a sequence starting from 0. > At least, that was my case when I was implementing MessagePack protocol. > And I ended up just doing this trivial check: array_values($array) === > $array. > -- César D. Rodas Open Source developer +595-983-161124 PGP: F9ED A265 A3AB C8A1 D145 7368 158A 0336 C707 0AA6