Skip to content

Instantly share code, notes, and snippets.

@agusputra
Last active January 28, 2022 23:35
Show Gist options
  • Select an option

  • Save agusputra/f1101b0bf74c6f4bbce8 to your computer and use it in GitHub Desktop.

Select an option

Save agusputra/f1101b0bf74c6f4bbce8 to your computer and use it in GitHub Desktop.
<?php
function flattenArray($source, $key = null)
{
$result = array();
if ($key) {
array_walk_recursive($source, function ($v, $k) use ($key, &$result) {
if ($k === $key) {
$result[] = $v;
}
});
} else {
array_walk_recursive($source, function ($v, $k) use (&$result) {
$result[] = $v;
});
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment