Advertisement
einpraegsam

Untitled

Jun 10th, 2016
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.85 KB | None | 0 0
  1. <?php
  2. namespace In2code\In2template\Utility;
  3.  
  4. use TYPO3\CMS\Core\Utility\ArrayUtility as ArrayUtilityCore;
  5.  
  6. /**
  7.  * Class ArrayUtility
  8.  */
  9. class ArrayUtility extends ArrayUtilityCore
  10. {
  11.     /**
  12.      * Extract array on dots
  13.      *
  14.      *  [
  15.      *      'key.key2' => 'value'
  16.      *  ]
  17.      *
  18.      *      =>
  19.      *
  20.      *  [
  21.      *      'key' =>
  22.      *      [
  23.      *          'key2' => 'value'
  24.      *      ]
  25.      *  ]
  26.      *
  27.      * @param array $array
  28.      * @return array
  29.      */
  30.     public static function resolveArrayPathsRecursive(array $array)
  31.     {
  32.         $newArray = [];
  33.         foreach ($array as $originalKey => &$value) {
  34.             if (is_array($value)) {
  35.                 $value = self::resolveArrayPathsRecursive($value);
  36.             }
  37.             if (stristr($originalKey, '.')) {
  38.                 $keys = explode('.', $originalKey);
  39.                 $currentKey = $keys[0];
  40.                 unset($keys[0]);
  41.                 if (!empty($keys)) {
  42.                     if (array_key_exists($currentKey, $newArray)) {
  43.                         self::mergeRecursiveWithOverrule(
  44.                             $newArray[$currentKey],
  45.                             self::resolveArrayPathsRecursive(
  46.                                 [implode('.', $keys) => $value]
  47.                             )
  48.                         );
  49.                     } else {
  50.                         $newArray[$currentKey] = self::resolveArrayPathsRecursive([implode('.', $keys) => $value]);
  51.                     }
  52.                 }
  53.             } else {
  54.                 if (array_key_exists($originalKey, $newArray)) {
  55.                     self::mergeRecursiveWithOverrule($newArray[$originalKey], $value);
  56.                 } else {
  57.                     $newArray[$originalKey] = $value;
  58.                 }
  59.             }
  60.         }
  61.         return $newArray;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement