keboola/php-utils

View on GitHub
src/Keboola/Utils/flattenArray.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Keboola\Utils;

/**
 * Take a multidimensional array and return a singledimensional one
 * Array keys are concatenated by "."
 * @param array $array
 * @param string $prefix Prefix the array key
 * @param string $glue
 * @return array
 */
function flattenArray(array $array, $prefix = "", $glue = '.')
{
    $result = [];
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $result = array_merge($result, flattenArray($value, $prefix . $key . $glue, $glue));
        } else {
            $result[$prefix . $key] = $value;
        }
    }
    return $result;
}