Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6893659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:41:11+00:00 2026-05-27T06:41:11+00:00

I’m writing a function named all to check all elements inside an array $arr

  • 0

I’m writing a function named all to check all elements inside an array $arr, returning a single boolean value (based of $f return value). This is working fine passing custom functions (see the code with $gte0 been passed to all).

However sometimes one want just check that an array contains all true values: all(true, $arr) will not work becase true is passed as boolean (and true is not a function name). Does PHP have a native true() like function?

function all($f, array $arr)
{
    return empty($arr) ? false : array_reduce($arr, function($v1, $v2) use ($f) {
        return $f($v1) && $f($v2);
    }, true);
}

$test = array(1, 6, 2);
$gte0 = function($v) { return $v >= 0; }

var_dump(all($gte0, $test)); // True

$test = array(true, true, false);
$id   = function($v) { return $v; } // <-- this is what i would avoid
var_dump(all($id, $test)); // False

all(true, $test); // NOT WORKING because true is passed as boolean
all('true', $test); // NOT WORKING because true is not a function

EDIT: another way could be checking $f in all function:

$f = is_bool($f) ? ($f ? function($v) { return $v; }
    : function($v) { return !$v; } ): $f;

After adding this, calling all with true is perfectly fine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:41:12+00:00Added an answer on May 27, 2026 at 6:41 am

    intval might do what you’re looking for (especially as the array only contains integers in your example):

    var_dump(all('intval', $test)); // False
    

    However, many types to integer conversions are undefined in PHP and with float this will round towards zero, so this might not be what you want.

    The more correct “function” would be the opposite of boolean true: empty, but it’s not a function, so you can’t use it (and invert the return value):

    var_dump(!all('empty', $test)); // Does not work!
    

    And there is no function called boolval or similar in PHP, so write it yourself if you need it 😉

    Additionally your all function could be optimized. While iterating, if the current result is already FALSE, the end result will always be FALSE. And no need to call $f() n * 2 times anyway:

    function all($f, array $arr)
    {
        $result = (bool) $arr;
        foreach($arr as $v) if (!$f($v)) return FALSE;
        return $result;
    }
    

    Edit: knittl is right pointing to array_filter, it converts to boolean with no function given which seems cool as there is no “boolval” function:

    function all($f, array $arr)
    {   
        return ($c = count($arr))
            && ($f ? $arr = array_map($f, $arr) : 1)
            && $c === count(array_filter($arr));
    }
    
    var_dump(all(0, $test)); // False
    

    Making the first function parameter optional will do you a proper bool cast on each array element thanks to array_filter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I am writing an app with both english and french support. The app requests
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.