Skip to content

Instantly share code, notes, and snippets.

@kfriend
Created May 22, 2013 04:27
Show Gist options
  • Save kfriend/5625241 to your computer and use it in GitHub Desktop.
Save kfriend/5625241 to your computer and use it in GitHub Desktop.
PHP flattening function. Turns a record into a dot-delimited array, converting both nest arrays and objects
<?php
function flatten($record, $prepend = '', $delimiter = '.')
{
$results = array();
foreach ($record as $key => $value)
{
if (is_array($value) or is_object($value))
{
$results = array_merge($results, flatten((array) $value, $prepend.$key.$delimiter));
}
else
{
$results[$prepend.$key] = $value;
}
}
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment