1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | function array_to_pipe( $array , $delimeter = '|' , $parents = array (), $recursive = false) { $result = '' ; foreach ( $array as $key => $value ) { $group = $parents ; array_push ( $group , $key ); // check if value is an array if ( is_array ( $value )) { if ( $merge = array_to_pipe( $value , $delimeter , $group , true)) { $result = $result . $merge ; } continue ; } // check if parent is defined if (! empty ( $parents )) { $result = $result . PHP_EOL . implode( $delimeter , $group ) . $delimeter . $value ; continue ; } $result = $result . PHP_EOL . $key . $delimeter . $value ; } // somehow the function outputs a new line at the beginning, we fix that // by removing the first new line character if (! $recursive ) { $result = substr ( $result , 1); } return $result ; } echo '<pre>' .array_to_pipe( $your_array ). '</pre>' ; |
Revisions: (Show)
No comments yet.