php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71320 Extend array_map/filter to call method on each element
Submitted: 2016-01-09 15:16 UTC Modified: 2019-12-01 04:22 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: dharkness at gmail dot com Assigned: cmb (profile)
Status: No Feedback Package: Arrays related
PHP Version: 5.6.17 OS:
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
37 + 20 = ?
Subscribe to this entry?

 
 [2016-01-09 15:16 UTC] dharkness at gmail dot com
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']);


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-09 15:39 UTC] dharkness at gmail dot com
After further consideration, perhaps this deserves new functions to avoid confusing the definition of a callable. They would behave just like these:

    function array_map_method(array $arr, $name) {
        return $arr 
            ? array_map(function ($object) use ($name) {
                    return $object->$name();
                }, $arr) 
            : array();
    }

and

    function array_filter_method(array $arr, $name) {
        return $arr 
            ? array_filter($arr, function ($object) use ($name) {
                    return $object->$name();
                }) 
            : array();
    }

May as well add array_map_property() and array_filter_property() for completeness. :)
 [2019-11-17 16:29 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2019-11-17 16:29 UTC] cmb@php.net
Hmm, wouldn't the newly introduced arrow functions offer a
sensible alternative to your suggestion?  For instance,

    $titles = array_map(fn($book) => $book->getTitle(), $books);

Of course, still some boilerplate code, but a lot more flexible
since properties can be read as well, for instance.
 [2019-12-01 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 09 04:01:27 2024 UTC