Created
August 22, 2012 17:05
-
-
Save evilbloodydemon/3427565 to your computer and use it in GitHub Desktop.
flatten array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $data = array( | |
| "Declarant" => array( | |
| "Governance" => array( | |
| "Name" => "Tommy", | |
| "LastName" => "Vercetti" | |
| ), | |
| ), | |
| 'a' => array( | |
| 'b' => array( | |
| 'c' => array( | |
| 'd' => 44, | |
| ), | |
| ), | |
| ), | |
| ); | |
| function flat($data, $path = '', $wrap = false) { | |
| $result = array(); | |
| foreach ($data as $k => $v) { | |
| if (is_array($v)) { | |
| $result += flat($v, $path . ($wrap ? "[$k]" : $k), true); | |
| } else { | |
| $result[$path . ($wrap ? "[$k]" : $k)] = $v; | |
| } | |
| } | |
| return $result; | |
| } | |
| var_dump(flat($data)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment