[PHP-NOTES] note 130261 added to function.array-filter

[email protected] ("[email protected]")
Newsgroups php.notes
Message-ID <[email protected]>
Please note that the examples above will result into error "E_DEPRECATED: strlen(): Passing null to parameter #1 ($string) of type string is deprecated" on PHP 8.1.0 and above.

<?php
declare(strict_types=1);

$array = array( 'null' => null, 'nullstring' => '',  'intzero' => 0,  'stringzero' => '0', 'false' => false, 'stringfalse' => 'false', );

// Removes null, null-string -- but also FALSE!
$filtered1 = array_filter( $array, 'strlen' );

?>

In order to use strlen on an array with null values, add/use strval, see the example below:

<?php
declare(strict_types=1);

$array = array( 'null' => null, 'nullstring' => '',  'intzero' => 0,  'stringzero' => '0', 'false' => false, 'stringfalse' => 'false', );

// Removes null, null-string -- but also FALSE!
$filtered1 = array_filter( $array, function($v) { return strlen(strval($v)); } );

?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 77.164.192.77)
----
Manual Page -- https://php.net/manual/en/function.array-filter.php
Edit        -- https://main.php.net/note/edit/130261
Del: integrated  -- https://main.php.net/note/delete/130261/integrated
Del: useless     -- https://main.php.net/note/delete/130261/useless
Del: bad code    -- https://main.php.net/note/delete/130261/bad+code
Del: spam        -- https://main.php.net/note/delete/130261/spam
Del: non-english -- https://main.php.net/note/delete/130261/non-english
Del: in docs     -- https://main.php.net/note/delete/130261/in+docs
Del: other reasons-- https://main.php.net/note/delete/130261
Reject      -- https://main.php.net/note/reject/130261
Search      -- https://main.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.