src/Utilities/Arrays.php
public static function getLastElementsPaths(array $array, $separator = '.', $parentPath = '', $stopIfMatchedBy = '')
    {
        /*
         * No elements?
         * Nothing to do
         */
        if (empty($array)) {
            return null;
        }

        if (!empty($stopIfMatchedBy)) {
            $stopIfMatchedBy = self::makeArray($stopIfMatchedBy);
        }

        $paths = [];

        foreach ($array as $key => $value) {
            $path = $key;
            $stopRecursion = false;
            $valueIsArray = is_array($value);

            /*
             * If the path of parent element is delivered,
             * I have to use it and build longer path
             */
            if (!empty($parentPath)) {
                $pathTemplate = '%s%s%s';
                $path = sprintf($pathTemplate, $parentPath, $separator, $key);
            }

            /*
             * Check if the key or current path matches one of patterns at which the process should be stopped,
             * the recursive not used. It means that I have to pass current value and stop processing of the
             * array (don't go to the next step).
             */
            if (!empty($stopIfMatchedBy)) {
                foreach ($stopIfMatchedBy as $rawPattern) {
                    $pattern = sprintf('|%s|', $rawPattern);

                    if (preg_match($pattern, $key) || preg_match($pattern, $path)) {
                        $stopRecursion = true;

                        break;
                    }
                }
            }

            /*
             * The value is passed to the returned array if:
             * - it's not an array
             * or
             * - the process is stopped, recursive is not used
             */
            if (!$valueIsArray || ($valueIsArray && empty($value)) || $stopRecursion) {
                $paths[$path] = $value;

                continue;
            }

            // Let's iterate through the next level, using recursive
            if ($valueIsArray) {
                $recursivePaths = self::getLastElementsPaths($value, $separator, $path, $stopIfMatchedBy);
                $paths += $recursivePaths;
            }
        }

        return $paths;
    }
public static function getLastElementsPaths(array $array, $separator = '.', $parentPath = '', $stopIfMatchedBy = '')
    {
        /*
         * No elements?
         * Nothing to do
         */
        if (empty($array)) {
            return null;
        }

        if (!empty($stopIfMatchedBy)) {
            $stopIfMatchedBy = self::makeArray($stopIfMatchedBy);
        }

        $paths = [];

        foreach ($array as $key => $value) {
            $path = $key;
            $stopRecursion = false;
            $valueIsArray = is_array($value);

            /*
             * If the path of parent element is delivered,
             * I have to use it and build longer path
             */
            if (!empty($parentPath)) {
                $pathTemplate = '%s%s%s';
                $path = sprintf($pathTemplate, $parentPath, $separator, $key);
            }

            /*
             * Check if the key or current path matches one of patterns at which the process should be stopped,
             * the recursive not used. It means that I have to pass current value and stop processing of the
             * array (don't go to the next step).
             */
            if (!empty($stopIfMatchedBy)) {
                foreach ($stopIfMatchedBy as $rawPattern) {
                    $pattern = sprintf('|%s|', $rawPattern);

                    if (preg_match($pattern, $key) || preg_match($pattern, $path)) {
                        $stopRecursion = true;

                        break;
                    }
                }
            }

            /*
             * The value is passed to the returned array if:
             * - it's not an array
             * or
             * - the process is stopped, recursive is not used
             */
            if (!$valueIsArray || ($valueIsArray && empty($value)) || $stopRecursion) {
                $paths[$path] = $value;

                continue;
            }

            // Let's iterate through the next level, using recursive
            if ($valueIsArray) {
                $recursivePaths = self::getLastElementsPaths($value, $separator, $path, $stopIfMatchedBy);
                $paths += $recursivePaths;
            }
        }

        return $paths;
    }
public static function getLastElementsPaths ( array $ array , $ separator = '.' , $ parentPath = '' , $ stopIfMatchedBy = '' ) { /*
         * No elements?
         * Nothing to do
         */ if ( empty ( $ array ) ) { return null ; } if ( ! empty ( $ stopIfMatchedBy ) ) { $ stopIfMatchedBy = self :: makeArray ( $ stopIfMatchedBy ) ; } $ paths = [ ] ; foreach ( $ array as $ key => $ value ) { $ path = $ key ; $ stopRecursion = false ; $ valueIsArray = is_array ( $ value ) ; /*
             * If the path of parent element is delivered,
             * I have to use it and build longer path
             */ if ( ! empty ( $ parentPath ) ) { $ pathTemplate = '%s%s%s' ; $ path = sprintf ( $ pathTemplate , $ parentPath , $ separator , $ key ) ; } /*
             * Check if the key or current path matches one of patterns at which the process should be stopped,
             * the recursive not used. It means that I have to pass current value and stop processing of the
             * array (don't go to the next step).
             */ if ( ! empty ( $ stopIfMatchedBy ) ) { foreach ( $ stopIfMatchedBy as $ rawPattern ) { $ pattern = sprintf ( '|%s|' , $ rawPattern ) ; if ( preg_match ( $ pattern , $ key ) || preg_match ( $ pattern , $ path ) ) { $ stopRecursion = true ; break ; } } } /*
             * The value is passed to the returned array if:
             * - it's not an array
             * or
             * - the process is stopped, recursive is not used
             */ if ( ! $ valueIsArray || ( $ valueIsArray && empty ( $ value ) ) || $ stopRecursion ) { $ paths [ $ path ] = $ value ; continue ; } // Let's iterate through the next level, using recursive if ( $ valueIsArray ) { $ recursivePaths = self :: getLastElementsPaths ( $ value , $ separator , $ path , $ stopIfMatchedBy ) ; $ paths += $ recursivePaths ; } } return $ paths ; }
Returns paths of the last elements

@param array        $array           The array with elements
@param string       $separator       (optional) Separator used between elements. Default: ".".
@param string       $parentPath      (optional) Path of the parent element. Default: "".
@param array|string $stopIfMatchedBy (optional) Patterns of keys or paths that matched will stop the process
of path building and including children of those keys or paths (recursive
will not be used for keys in lower level of given array). Default: "".
@return null|array

Examples - $stopIfMatchedBy argument:
a) "\d+"
b) [
"lorem\-",
"\d+",
];
Returns paths of the last elements