Verified Commit 19052a6b authored by Cyril Vazquez's avatar Cyril Vazquez
Browse files

Function to flatten data structures

parent 1534b913
Pipeline #14672 failed with stages
in 54 seconds
......@@ -1448,4 +1448,37 @@ class laabs
$message = $source.' '.$delay.'s '.round(memory_get_usage()/1000000).'/'.round(memory_get_peak_usage()/1000000).'Mb';
static::log($message);
}
/**
* Flatten a data structure
* @param mixed $data
*
* @return array
*/
public static function flatten($data, &$flat = [], $path = null)
{
if ($path) {
$flat[$path] = $data;
}
if (is_object($data)) {
if (!empty($path)) {
$path.='.';
}
foreach ($data as $name => $value) {
static::flatten($value, $flat, $path.$name);
}
return $flat;
}
if (is_array($data)) {
foreach ($data as $key => $value) {
static::flatten($value, $flat, $path.'['.$key.']');
}
return $flat;
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment