Filter an Array by given prefixes

@param array $arrData
@param array $arrPrefixes

@return array the filtered array or $arrData if $strPrefix is empty
557254
public static function filterByPrefixes(array $arrData = [], $arrPrefixes = [])
    {
        $arrExtract = [];

        if (!is_array($arrPrefixes) || empty($arrPrefixes))
        {
            return $arrData;
        }

        foreach ($arrData as $key => $value)
        {
            foreach ($arrPrefixes as $strPrefix)
            {
                if (\HeimrichHannot\Haste\Util\StringUtil::startsWith($key, $strPrefix))
                {
                    $arrExtract[$key] = $value;
                }
            }
        }

        return $arrExtract;
    }
552180
private function extractValues(array $values, $prefix, $removePrefixFromKeys = true)
    {
        if (empty($values)) {
            return array();
        }

        $validKeys = array_filter(array_keys($values), function($name) use ($prefix) {
            return (0 === strpos($name, $prefix));
        });

        $results = array_intersect_key($values, array_flip($validKeys));
        if (!$removePrefixFromKeys) {
            return $results;
        }

        return array_combine(
            array_map(function($key) use ($prefix) {
                return str_replace($prefix, '', $key);
            }, array_keys($results)), $results
        );
    }
1236638
public static function flattenArray(array $array, $prefix = '')
    {
        $result = [];

        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $result = $result + self::flattenArray($value, $prefix.$key.'.');
            } else {
                $result[$prefix.$key] = self::normalizeValue($value);
            }
        }

        return $result;
    }
904291
private function checkPrefixes(array $paths)
    {
        sort($paths);

        for ($i = 1; $i < count($paths); $i++) {
            $prefix = $paths[$i-1];
            $suffix = $paths[$i];

            $prefix = explode('.', $prefix);
            $suffix = explode('.', $suffix);

            $isPrefix = count($prefix) < count($suffix)
                && $prefix === array_slice($suffix, 0, count($prefix));

            if ($isPrefix) {
                throw new \InvalidArgumentException(sprintf(
                    'Field path conflict detected for field path `%s`. ' .
                    'Conflicts occur when a field path descends from another ' .
                    'path. For instance `a.b` is not allowed when `a` is also ' .
                    'provided.',
                    $prefix
                ));
            }
        }
    }
878041
protected function filterArray($rules, $rawAttributes)
    {
        $attributes = [];
        foreach (array_keys($rules) as $filedName) {
            $value = array_get($rawAttributes, $filedName, 'not found in array'); // Default value !== null
            if ($value !== 'not found in array') { // Only if field specified in incoming array
                if (isset($this->filters[$filedName])) {
                    $filters = explode('|', $this->filters[$filedName]);
                    foreach ($filters as $filter) {
                        array_set($attributes, $filedName, $this->$filter($value));
                    }
                } else {
                    array_set($attributes, $filedName, $value);
                }
            }
        }
        return $attributes;
    }
800312
public static function filter_keys($arr, $keys)
    {
        foreach ($arr as $key => $v) {
            if (!in_array($key, $keys)) {
                unset($arr[$key]);
            }
        }

        return $arr;
    }
1051717
protected function filterPrefix(array $arguments)
    {
        if (empty($arguments)) {
            throw new InvalidArgumentException('The arguments cannot be empty');
        }

        $path = array_shift($arguments);

        if (! Assert::stringNotEmpty($path)) {
            throw new InvalidArgumentException(
                sprintf('"%s" path must be string', __METHOD__)
            );
        }

        list($prefix, $path) = $this->getPrefixAndPath($path);
        array_unshift($arguments, $path);
        unset($path);

        return [$prefix, $arguments];
    }
1096748
public function getPrefixString($array, $prefix)
    {
        $items = array();

        if(!empty($array)){
            foreach($array as $item){
                preg_match('/'.$prefix.'(.*)/', $item, $results);
                if(isset($results[1])){
                    $items[] = $results[1];
                }
            }
        }

        return $items;
    }
155436
private function flatten(array $array, string $prefix = ''): array
    {
        $result = [];
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $result = array_merge(
                    $result,
                    $this->flatten(
                        $value,
                        $prefix . rtrim($key, '.') . '.'
                    )
                );
            } else {
                $result[$prefix . $key] = $value;
            }
        }
        return $result;
    }
555807
public static function array2UriParams(array $arrParams) : string
    {
        $arrKeyVal = [];
        foreach ($arrParams as $key => $value) {
            $arrKeyVal[] = urlencode($key)."=".urlencode($value);
            //Not using slugify, want to be able to have php decode
        }
        $arrReturn = implode("&", $arrKeyVal);
        return $arrReturn;
    }
1190375
function extract($arr,$prefix) {
		$out=[];
		foreach (preg_grep('/^'.preg_quote($prefix,'/').'/',array_keys($arr))
			as $key)
			$out[substr($key,strlen($prefix))]=$arr[$key];
		return $out;
	}
51676
public function setupWithArray(Array $arrData){
		foreach($arrData as $strProperty => $mixOptions){
			if(!is_array($mixOptions)){
				$strProperty = $mixOptions;
				$mixOptions = array();
			}

			$strProperty = $this->_formatPropertyName($strProperty);
			$this->_arrProperties[] = $strProperty;
			$this->_arrOptions[$strProperty] = new SynthesizeOption($mixOptions);
			$this->_arrData[$strProperty] = $this->_create($this->_arrOptions[$strProperty]);
		}
	}
458732
private function applyReadPermissionsToCategories(Array $arrCategories)
	{
		$arrSecureCategories = $arrCategories;
		foreach ($arrSecureCategories as $category)
		{
			if (!$category->isPublished() || ($this->dmsHideEmptyCategories && !$category->hasPublishedDocuments()) || ($this->dmsHideLockedCategories && !$category->isReadableForCurrentMember()))
			{
				unset($arrSecureCategories[$category->id]);
			}
			else if ($category->hasSubCategories())
			{
				$arrSecureSubCategories = $this->applyReadPermissionsToCategories($category->subCategories);
				$category->subCategories = $arrSecureSubCategories;
			}
		}
		return $arrSecureCategories;
	}
1033343
private function arrayFilterRecursive(array &$array)
    {
        foreach ($array as $key => &$item) {
            is_array($item) && $array[$key] = $this->arrayFilterRecursive($item);
            if ($array[$key] === null ||
                $array[$key] === []
            ) {
                unset($array[$key]);
            }
        }
        return $array;
    }
850572
public static function lineMatch(
        array $lines,
        string $prefix,
        string $suffix,
        bool $doTrim,
        bool $removePrefixSuffix = false
    ): array {
        $result = [];
        foreach ($lines as $line) {
            $isHit = true;
            if ($prefix !== '' && $prefix !== null && Str::startsWith(trim($line), $prefix)) {
                if ($removePrefixSuffix) {
                    $line = substr(trim($line), strlen($prefix));
                }
            } else {
                $isHit = false;
            }
            if ($suffix !== '' && $suffix !== null && Str::endsWith(trim($line), $suffix)) {
                if ($removePrefixSuffix) {
                    $line = substr(trim($line), 0, -strlen($suffix));
                }
            } else {
                $isHit = false;
            }
            if ($isHit) {
                if ($doTrim) {
                    $line = trim($line);
                }
                $result[] = $line;
            }
        }
        return $result;
    }
1112302
public function resolveMessageArrayToMessageKeys(array $messages, $prefix="")
    {
        $flatArray = [];

        foreach ($messages as $key=>$message) {
            $this->resolveMessageToKeys($message, $key, function($qualifiedKey) use(&$flatArray)
                {
                    $flatArray[] = $qualifiedKey;
                }, $prefix);
        }

        return $flatArray;
    }
836433
public static function prepareHeaderArrayForPrint(array $arrHeaders)
    {
        $strResult = '';
        $i = 0;

        foreach ($arrHeaders as $strKey => $strValue)
        {
            $strResult .= "$strKey: $strValue";

            if ($i++ != count($arrHeaders) - 1)
            {
                $strResult .= PHP_EOL;
            }
        }

        return $strResult;
    }