Req #54076 [Opn->Wfx]: Feature request - function array_walk_recursive
| From: | cmb@php.net | Date: | Tue, 31 Aug 2021 10:26:01 +0000 |
| Subject: | Req #54076 [Opn->Wfx]: Feature request - function array_walk_recursive | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-236194@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=54076&edit=1
ID: 54076
Updated by: cmb@php.net
Reported by: Tor-Martin at storsletten dot com
Summary: Feature request - function array_walk_recursive
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Arrays related
Operating System: All
PHP Version: 5.3.5
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
This doesn't look like it would be needed that often, that it is
not better implemented in userland, and the lack of comments and
upvotes seems to confirm that. Thus, I'm closing as WONTFIX.
If you're still interested in this feature, please pursue the RFC
process[1].
[1] <https://wiki.php.net/rfc/howto>
Previous Comments:
------------------------------------------------------------------------
[2011-02-22 21:56:11] Tor-Martin at storsletten dot com
Description:
------------
---
From manual page: http://www.php.net/function.array-walk-recursive#Parameters
---
This function does not send arrays to the callback function, and in most cases this is what you
want, but sometimes you may need at least the keys of the arrays (example below). My suggestion is
to add a fourth parameter to this function which takes a boolean value, and defaults to FALSE. The
callback function can then handle the arrays and their keys if this parameter is set to TRUE.
One usage for this feature would be if you want to make a custom function for serializing an array
and all its content. A serialized string must contain the keys of child arrays, else the custom
unserializer will be unable to restore child arrays properly. A custom serializer may be able to
make binary strings rather than human-readable strings like serialize() does, which will save a lot
of space e.g. when storing $_SESSION data in a database.
Additionally, this feature would also allow custom output of arrays in a human-friendly way,
especially if the callback function can get the depth of the iteration (see the code example below).
Test script:
---------------
function awr_callback(&$val, &$key, &$ptr, $depth = 0) {
$indent = str_pad('', $depth, ' ');
if (is_array($val)) $ptr[0] .= "${indent}Array '$key' (" . count($val) .
"):\n";
elseif (is_string($val)) $ptr[0] .= "${indent}String '$key' (" . strlen($val) .
"): $val\n";
// ...
}
$a = array('beginning', 'first child' => array('an element',
'another element'), 'end');
$s = '';
array_walk_recursive($a, 'awr_callback', array(&$s), true);
echo $s;
// This short example is written to demonstrate usage of the suggested features, and will not work
with the current version of PHP.
Expected result:
----------------
Expected output when/if the features are implemented:
String '0' (9): beginning
Array 'first child' (2):
String '0' (10): an element
String '1' (15): another element
String '1' (3): end
Actual result:
--------------
Actual output (if you first remove the fourth input parameter when calling array_walk_recursive):
String '0' (9): beginning
String '0' (10): an element
String '1' (15): another element
String '1' (3): end
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=54076&edit=1