php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #8953 in_array returning number of 'hits'
Submitted: 2001-01-27 15:16 UTC Modified: 2001-01-29 09:12 UTC
From: melvyn at idg dot nl Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.4pl1 OS: any
Private report: No CVE-ID: None
 [2001-01-27 15:16 UTC] melvyn at idg dot nl
I'd like to see in_array returning the number of values matching, instead of just true/false.

IMHO, it would not create any backwards compatibility problems, since 0 equals false anyway and > 0 equals true.

It would make constructing dual-parameter descisions much easier.

For example - a bounce script, where "bounce_count / day" > 6 AND "bounces /week" = 5.

This is very hard to do now, but with in_array returning the number of hits, it takes out at least 2 loops and a seperate "counts" array.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-01-29 09:12 UTC] andrei@php.net
I would not want to do this, because it will slow down the execution of the scripts on average, since in_array() will have to traverse the entire array to produce the count you want, instead of just stopping at the first match.

Try using count(array_keys($array, "foo")) - this will return the number of times "foo" occurs in $array.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 02:01:29 2024 UTC