Skip to content

Instantly share code, notes, and snippets.

@maxcelos
Created December 29, 2021 13:01
Show Gist options
  • Select an option

  • Save maxcelos/6f1b8acd45d97068bceb4c5d4f5f61da to your computer and use it in GitHub Desktop.

Select an option

Save maxcelos/6f1b8acd45d97068bceb4c5d4f5f61da to your computer and use it in GitHub Desktop.

Revisions

  1. maxcelos created this gist Dec 29, 2021.
    20 changes: 20 additions & 0 deletions php_array_flatten.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    ```php
    flatten(array $array, $keyPrefix = ''): array
    {
    $return = [];

    foreach ($array as $key => $value) {
    $key = trim($keyPrefix . '_' . $key, '_');

    if (is_array($value)){
    $return = array_merge($return, flatten($value, $key));
    } else {
    $return[$key] = $value;
    }
    }

    return $return;
    }
    ```

    ![Screen Shot 2021-12-29 at 09 59 28](https://user-images.githubusercontent.com/15364289/147664942-93631313-f86a-4425-95f4-2fd28f810966.png)