array_flatten_with_dots example

// Run through each rule. If we have any field set for         // this rule, then we need to run them through!         foreach ($this->rules as $field => $setup) {
            $rules = $setup['rules'];

            if (is_string($rules)) {
                $rules = $this->splitRules($rules);
            }

            if (strpos($field, '*') !== false) {
                $values = array_filter(array_flatten_with_dots($data)static fn ($key) => preg_match(
                    '/^'
                    . str_replace(['\.\*', '\*\.']['\..+', '.+\.']preg_quote($field, '/'))
                    . '$/',
                    $key
                ), ARRAY_FILTER_USE_KEY);
                // if keys not found                 $values = $values ?: [$field => null];
            } else {
                $values = dot_array_search($field$data);
            }

            

    function array_flatten_with_dots(iterable $array, string $id = ''): array
    {
        $flattened = [];

        foreach ($array as $key => $value) {
            $newKey = $id . $key;

            if (is_array($value) && $value !== []) {
                $flattened = array_merge($flattenedarray_flatten_with_dots($value$newKey . '.'));
            } else {
                $flattened[$newKey] = $value;
            }
        }

        return $flattened;
    }
}

if (function_exists('array_group_by')) {
    /** * Groups all rows by their index values. Result's depth equals number of indexes * * @param array $array Data array (i.e. from query result) * @param array $indexes Indexes to group by. Dot syntax used. Returns $array if empty * @param bool $includeEmpty If true, null and '' are also added as valid keys to group * * @return array Result array where rows are grouped together by indexes values. */
Home | Imprint | This part of the site doesn't use cookies.