Adding flatten function to array helper. #1620

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
5 participants

Often times in manipulating arrays all the leaf elements are desired, an example is given below. Using a flattening function can do this.

Argument:

  0 => 
    array
      0 => int 4124
  1 => 
    array
      0 => int 1224
  2 => 
    array
      0 => int 2129
  3 => 
    array
      0 => int 8194
  4 => 
    array
      0 => int 3125
  5 => 
    array
      0 => int 1121
  6 => 
    array
      0 => int 5174

Result:

  0 => int 4124
  1 => int 1224
  2 => int 2129
  3 => int 8194
  4 => int 3125
  5 => int 1121
  6 => int 5174

@it-can it-can commented on the diff Jul 14, 2012

system/helpers/array_helper.php
@@ -102,5 +102,28 @@ function elements($items, $array, $default = NULL)
}
}
+if ( ! function_exists('flatten'))
+{
+ /**
+ * Flatten
+ *
+ * Returns all of the leaves of an array, flattening it.
+ *
+ * @param array
+ * @return array leaves of the array
+ */
+ function flatten(array $array)
+ {
@it-can

it-can Jul 14, 2012

Contributor

Seems you have some spacing problems...?

Contributor

daparky commented Aug 8, 2012

+1

Contributor

timw4mail commented Aug 8, 2012

This implmentation won't fly - it requires php 5.3

narfbg closed this Oct 23, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment