[FEATURE] Add possibility to flatten array
authorOliver Hader <oliver@typo3.org>
Sun, 19 Aug 2012 09:55:44 +0000 (11:55 +0200)
committerSusanne Moog <typo3@susannemoog.de>
Sun, 19 Aug 2012 10:02:26 +0000 (12:02 +0200)
A commonly used task in TYPO3 is to convert and work with
several types of arrays. This change allows to flatten a
nested multidimensional array to a flat key-value array.

array('first.' => array('second' => 1))
will become
array('first.second' => 1)

Change-Id: Ia41c53b87ac984bb6b29741ce461e4af70b6215f
Resolves: #39951
Releases: 6.0
Reviewed-on: http://review.typo3.org/13910
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
t3lib/utility/class.t3lib_utility_array.php
tests/Unit/t3lib/utility/class.t3lib_utility_arrayTest.php

index bc0295e..2a3976e 100644 (file)
@@ -325,6 +325,37 @@ class t3lib_utility_Array {
                $lines .= str_repeat(TAB, $level - 1) . ')' . ($level - 1 == 0 ? '' : ',' . LF);
                return $lines;
        }
+
+       /**
+        * Converts a multidimensional array to a flat representation.
+        *
+        * array('first.' => array('second' => 1)) and array('first' => array('second' => 1))
+        * will become
+        * array('first.second' => 1)
+        *
+        * @param array $array The (relative) array to be converted
+        * @param string $prefix The (relative) prefix to be used (e.g. 'section.')
+        * @return array
+        */
+       public static function flatten(array $array, $prefix = '') {
+               $flatArray = array();
+
+               foreach ($array as $key => $value) {
+                               // Ensure there is no trailling dot:
+                       $key = rtrim($key, '.');
+
+                       if (!is_array($value)) {
+                               $flatArray[$prefix . $key] = $value;
+                       } else {
+                               $flatArray = array_merge(
+                                       $flatArray,
+                                       self::flatten($value, $prefix . $key . '.')
+                               );
+                       }
+               }
+
+               return $flatArray;
+       }
 }
 
 ?>
\ No newline at end of file
index 45f0c45..699c9d0 100644 (file)
@@ -759,6 +759,111 @@ class t3lib_utility_ArrayTest extends tx_phpunit_testcase {
                ')';
                $this->assertSame($expected, t3lib_utility_Array::arrayExport($array));
        }
+
+       /**
+        * @param array $array
+        * @param array $expected
+        * @test
+        * @dataProvider arrayIsFlatDataProvider
+        */
+       public function arrayIsFlat(array $array, array $expected) {
+               $this->assertEquals(
+                       $expected,
+                       t3lib_utility_Array::flatten($array)
+               );
+       }
+
+       /**
+        * @return array
+        */
+       public function arrayIsFlatDataProvider() {
+               return array(
+                       'plain array' => array(
+                               array(
+                                       'first' => 1,
+                                       'second' => 2,
+                               ),
+                               array(
+                                       'first' => 1,
+                                       'second' => 2,
+                               ),
+                       ),
+                       'plain array with faulty dots' => array(
+                               array(
+                                       'first.' => 1,
+                                       'second.' => 2,
+                               ),
+                               array(
+                                       'first' => 1,
+                                       'second' => 2,
+                               ),
+                       ),
+                       'nested array of 2 levels' => array(
+                               array(
+                                       'first.' => array(
+                                               'firstSub' => 1,
+                                       ),
+                                       'second.' => array(
+                                               'secondSub' => 2,
+                                       ),
+                               ),
+                               array(
+                                       'first.firstSub' => 1,
+                                       'second.secondSub' => 2,
+                               ),
+                       ),
+                       'nested array of 2 levels with faulty dots' => array(
+                               array(
+                                       'first.' => array(
+                                               'firstSub.' => 1,
+                                       ),
+                                       'second.' => array(
+                                               'secondSub.' => 2,
+                                       ),
+                               ),
+                               array(
+                                       'first.firstSub' => 1,
+                                       'second.secondSub' => 2,
+                               ),
+                       ),
+                       'nested array of 3 levels' => array(
+                               array(
+                                       'first.' => array(
+                                               'firstSub.' => array(
+                                                       'firstSubSub' => 1,
+                                               ),
+                                       ),
+                                       'second.' => array(
+                                               'secondSub.' => array(
+                                                       'secondSubSub' => 2,
+                                               ),
+                                       ),
+                               ),
+                               array(
+                                       'first.firstSub.firstSubSub' => 1,
+                                       'second.secondSub.secondSubSub' => 2,
+                               ),
+                       ),
+                       'nested array of 3 levels with faulty dots' => array(
+                               array(
+                                       'first.' => array(
+                                               'firstSub.' => array(
+                                                       'firstSubSub.' => 1,
+                                               ),
+                                       ),
+                                       'second.' => array(
+                                               'secondSub.' => array(
+                                                       'secondSubSub.' => 2,
+                                               ),
+                                       ),
+                               ),
+                               array(
+                                       'first.firstSub.firstSubSub' => 1,
+                                       'second.secondSub.secondSubSub' => 2,
+                               ),
+                       ),
+               );
+       }
 }
 
 ?>
\ No newline at end of file