- array_reduce: documentation ( source)
- array_reverse: documentation ( source)
- print_r: documentation ( source)
<?php
$keys = [ 'my', 'name', 'is', 'Rafael' ];
$value = 'value';
$result = array_reduce( array_reverse( $keys ), function ( $c, $i ) use ( $value ) {
return [ $i => ( is_null( $c ) ? $value : $c ) ];
} );
print_r( $result );
// или
$result = array_reduce( array_reverse( $keys ), fn( $c, $i ) => [ $i => $c ?? $value ] );
print_r( $result );