- strpos: documentation ( source)
- print_r: documentation ( source)
- array_shift: documentation ( source)
- explode: documentation ( source)
- array_pop: documentation ( source)
<?php
function combineColumns(array $row, $separator) // {{{
{
$combined = array();
foreach ($row as $key => $value) {
if (false !== strpos($key, $separator)) {
$parts = explode($separator, $key);
$pkey = array_pop($parts);
$ptr = &$combined;
while (null !== ($part = array_shift($parts))) {
if (!isset($ptr[$part]) || !is_array($ptr[$part])) {
$ptr[$part] = array();
}
$ptr = &$ptr[$part];
}
$ptr[$pkey] = $value;
} else {
$combined[$key] = $value;
}
}
return $combined;
}
print_r(combineColumns(array(
'A' => 1,
'B_C' => 2,
'B_D_A' => 3,
'D_K_F' => 4,
), '_'));