I have this type of array what contains total income in each month:
Array ( [Total_Income_JAN] => 5000 [Total_Income_FEB] => 8000 [Total_Income_MAR] => 10000 )
How can I remove the prefix part from the array keys so that it becomes:
Array ( [JAN] => 4321 [FEB] => 2364 [MAR] => 2364 )
Try this:
<?php
$list = Array("Total_Income_JAN" => 5000, "Total_Income_FEB" => 8000 , "Total_Income_MAR" => 10000);
function removePrefix(array $input) {
$return = array();
foreach ($input as $key => $value) {
if (strpos($key, 'Total_Income_') === 0)
$key = substr($key, 13);
if (is_array($value))
$value = removePrefix($value);
$return[$key] = $value;
}
return $return;
}
$list = removePrefix($list);
print_r($list);
?>
Try Demo>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With