Req #71320 [NEW]: Extend array_map/filter to call method on each element

From: Date: Sat, 09 Jan 2016 15:16:28 +0000
Subject: Req #71320 [NEW]: Extend array_map/filter to call method on each element
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198546@lists.php.net to get a copy of this message
From:             dharkness at gmail dot com
Operating system: 
PHP version:      5.6.17
Package:          Arrays related
Bug Type:         Feature/Change Request
Bug description:Extend array_map/filter to call method on each element

Description:
------------
I'm finding myself writing code like the following lately:

    $titles = array_map(
        function ($book) { return $book->getTitle(); },
        $books
    );

and

    $anonymous = array_filter(
        $books,
        function ($book) { return $book->isAnonymous(); }
    );

I was initially thinking a new callable form word work, e.g. [null,
'methodName'], but that by itself isn't callable and would not make
sense when passed to other methods that accept callable such as
call_user_func. The functions would either build a real callable using
each array element or simply call the method directly on it.

    $titles = array_map([null, 'getTitle'], $books);

and

    $anonymous = array_filter($books, [null, 'isAnonymous']);


-- 
Edit bug report at https://bugs.php.net/bug.php?id=71320&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=71320&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=71320&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=71320&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=71320&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=71320&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=71320&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=71320&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=71320&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=71320&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=71320&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=71320&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=71320&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=71320&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71320&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=71320&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=71320&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=71320&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71320&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=71320&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=71320&r=mysqlcfg



Thread (4 messages)

« previous php.bugs (#198546) next »