Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91123 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 76110 invoked from network); 8 Feb 2016 17:04:10 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Feb 2016 17:04:10 -0000 Authentication-Results: pb1.pair.com header.from=gen.work@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=gen.work@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.47 as permitted sender) X-PHP-List-Original-Sender: gen.work@gmail.com X-Host-Fingerprint: 209.85.215.47 mail-lf0-f47.google.com Received: from [209.85.215.47] ([209.85.215.47:35341] helo=mail-lf0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6D/E5-36326-98AC8B65 for ; Mon, 08 Feb 2016 12:04:09 -0500 Received: by mail-lf0-f47.google.com with SMTP id l143so99584746lfe.2 for ; Mon, 08 Feb 2016 09:04:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=pEgl4U9LZchjFJ4FPuq/5AFVR9Xp+NWjyLDlwPyV4EI=; b=H8uS8UU01XBXhC696JnBqPCrDq/S4t5GD7up7SKQy4NlKMNyy27zedNNbiQMK3IZQT YdB9rWsqtqAta2u/aBFx3ySwAWQxryH14tQQMDHzpMoDYCACxYazv3K8TJnR0CJeGwl+ /H1ix5qZtkkE/4SZ9WlVLvUcaNWXX3qJ+OxToB7onrMczdteH93BDUCFlgN7W3yxZ5SJ kWkkkoQO0DEXiY1+a24HEqXZH2wgvgUzpHIB6hTs9IBekBi71paVCBj/HEuz5s1jhgAm ub/BggEl5Jo970KwJMhsOmF4wBr7E8VLub7LdqOml7b+IJZAXrqN+rORBM8TFlqznpTT 7xqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=pEgl4U9LZchjFJ4FPuq/5AFVR9Xp+NWjyLDlwPyV4EI=; b=PgR6P47yPVoSNLfrUSCDlndCzS3ujfOat7anKnho/pL04Lr30N1zVyxwm7UIJLF7mB 5o8ao/pfv447IbkcRq3B1kDbZVa8esedDS6Ozq5lCMV6Mu27SKcd+j9Zdv1U3EjlXT9f 3ikY1LUJGS7w2X87ARO6wIDWhG2LfBvMbBRQ5lsnkNX8wg62kCFS++XqKUrzFriZK9uL iArrso8yWS0pZ7Js4c5q2sbfpz6378GmI4wyMlV3SLJ6LG7/ce3pel+0nHl6NP09MPjh UKm5nXQIaRQkV5PONtJAeBP9Oraa2fdw45MmKb8zNg4ce/X0iu2HhjbI57gWW7Xwmw/V oO6g== X-Gm-Message-State: AG10YOQnmZoKswkmvsIKtmM4CS4FV3Is6X4hVe7KgX+4/QQQRPoQfHVwQxcybrSE/+n4Y9kaxxUjEkr7cAXZuA== MIME-Version: 1.0 X-Received: by 10.25.170.129 with SMTP id t123mr12102756lfe.103.1454951046360; Mon, 08 Feb 2016 09:04:06 -0800 (PST) Received: by 10.25.208.145 with HTTP; Mon, 8 Feb 2016 09:04:06 -0800 (PST) In-Reply-To: References: Date: Mon, 8 Feb 2016 18:04:06 +0100 Message-ID: To: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a1141121048b823052b45305b Subject: Re: [PHP-DEV] Proposal for a new array function From: gen.work@gmail.com (Eugene) --001a1141121048b823052b45305b Content-Type: text/plain; charset=UTF-8 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. > > 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. -- Thank you and best regards, Eugene Leonovich --001a1141121048b823052b45305b--