I want to empty all values by empty string in a PHP array, and keeping all the keys names recursively.
Example:
<?php
$input =
['abc'=> 123,
'def'=> ['456', '789', [
'ijk' => '555']
]
];
I want my array to become like this:
<?php
$output = ['abc'=> '',
'def'=> ['', '', [
'ijk' => '']
]
];
You should use recursive function:
function setEmpty($arr)
{
$result = [];
foreach($arr as $k=>$v){
/*
* if current element is an array,
* then call function again with current element as parameter,
* else set element with key $k as empty string ''
*/
$result[$k] = is_array($v) ? setEmpty($v) : '';
}
return $result;
}
And just call this function with your array as the only parameter:
$input = [
'abc' => 123,
'def' => [
'456',
'789', [
'ijk' => '555',
],
],
];
$output = setEmpty($input);