Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
function foreach_recursive_callback( array $array, callable $callback ) {
$result = [];
foreach ( $array as $key => $item ) {
$result[$key] = $callback( $item, $key );
if ( is_array( $item ) ) {
$result[$key] = foreach_recursive_callback( $item, $callback );
}
}
return $result;
}
$array = [
'foo' => 'bar',
'attr' => 'baz',
[
'a' => 'b',
'attr' => 'biz'
],
];
foreach_recursive_callback( $array, function( $item, $key ) {
if ( $key === 'attr' ) {
$item = ' '.$item;
}
return $item;
});
var_dump( $array );
//