Advertisement
Guest User

Untitled

a guest
May 28th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! function_exists( 'array_unflatten' ) )
  4. {
  5. /**
  6. * Convert flatten collection (with dot notation) to multiple dimmensionals array
  7. * @param Collection $collection Collection to be flatten
  8. * @return Array
  9. */
  10. function array_unflatten( $collection )
  11. {
  12. $collection = (array) $collection;
  13.  
  14. $output = array();
  15.  
  16. foreach ( $collection as $key => $value )
  17. {
  18. array_set( $output, $key, $value );
  19.  
  20. if ( is_array( $value ) && ! strpos( $key, '.' ) )
  21. {
  22. $nested = array_unflatten( $value );
  23.  
  24. $output[$key] = $nested;
  25. }
  26. }
  27.  
  28. return $output;
  29. }
  30. }
  31.  
  32.  
  33. // From Laravel
  34. if ( ! function_exists( 'array_set' ) )
  35. {
  36. function array_set( &$array, $key, $value )
  37. {
  38. if ( is_null( $key ) )
  39. return $array = $value;
  40.  
  41. $keys = explode( '.', $key );
  42.  
  43. while ( count( $keys ) > 1 )
  44. {
  45. $key = array_shift( $keys );
  46.  
  47. // If the key doesn't exist at this depth, we will just create an empty array
  48. // to hold the next value, allowing us to create the arrays to hold final
  49. // values at the correct depth. Then we'll keep digging into the array.
  50. if ( ! isset( $array[$key] ) || ! is_array( $array[$key] ) )
  51. {
  52. $array[$key] = array();
  53. }
  54.  
  55. $array =& $array[$key];
  56. }
  57.  
  58. $array[array_shift( $keys )] = $value;
  59.  
  60. return $array;
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement