Recursively replace keys of associative arrays

@param array $data
@param string $search
@param string $replace
@return array
761755
final protected static function replaceKey($data, $search, $replace)
    {
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                $data[$key] = $value = self::replaceKey($data[$key], $search, $replace);
            }
            if ($key == $search) {
                $data[$replace] = $value;
                unset($data[$search]);
            }
        }

        return $data;
    }
48646
private function replace($bindings)
    {
        $search = array_keys($bindings);
        $replace = array_values($bindings);

        foreach ($search as $key => &$value) {
            $value = $this->delimiters[0].$value.$this->delimiters[1];
        }

        array_walk_recursive($this->rules, function(&$value, $key) use($search, $replace) {
            $value = str_ireplace($search, $replace, $value);
        });
    }
485903
private function changePlacesUpdate(Tree $tree, string $search, string $replace): array
    {
        $individual_changes = DB::table('individuals')
            ->where('i_file', '=', $tree->id())
            ->whereContains('i_gedcom', $search)
            ->select(['individuals.*'])
            ->get()
            ->map(Individual::rowMapper());

        $family_changes = DB::table('families')
            ->where('f_file', '=', $tree->id())
            ->whereContains('f_gedcom', $search)
            ->select(['families.*'])
            ->get()
            ->map(Family::rowMapper());

        return $individual_changes
            ->merge($family_changes)
            ->mapWithKeys(static function (GedcomRecord $record) use ($search, $replace): array {
                $changes = [];

                foreach ($record->facts() as $fact) {
                    $old_place = $fact->attribute('PLAC');
                    if (preg_match('/(^|, )' . preg_quote($search, '/') . '$/i', $old_place)) {
                        $new_place           = preg_replace('/(^|, )' . preg_quote($search, '/') . '$/i', '$1' . $replace, $old_place);
                        $changes[$old_place] = $new_place;
                        $gedcom              = preg_replace('/(\n2 PLAC (?:.*, )*)' . preg_quote($search, '/') . '(\n|$)/i', '$1' . $replace . '$2', $fact->gedcom());
                        $record->updateFact($fact->id(), $gedcom, false);
                    }
                }

                return $changes;
            })
            ->sort()
            ->all();
    }
1084483
public function replace($search, $replace): Aggregator
    {
        foreach ($this->elements as $element) {
            if ($element instanceof ReplaceableInterface) {
                $element->replace($search, $replace);
            }
        }

        return $this;
    }
1330813
public function replaceEnding($search, $replacement)
    {
        return $this->regexReplace
        (
            preg_quote($search, '/').'$',
            UTF8::str_replace('\\', '\\\\', $replacement)
        );
    }
840098
protected function replaceWithData($text, array $matches, array $data)
    {
        list($search, $replace) = $this->getSearchAndReplace($matches, $data);

        return str_replace($search, $replace, $text);
    }
383035
public function replaceOutsideQuotes($search, $replace) {
        
        $this->search = $search;
        $this->replace = $replace;
        return $this->_strReplaceOutsideQuotes();
    }
1171044
public function replaceText($search, $replace)
    {
        if (!$this->open('r+')) {
            return false;
        }

        if ($this->lock !== null && flock($this->handle, LOCK_EX) === false) {
            return false;
        }

        $replaced = $this->write(str_replace($search, $replace, $this->read()), 'w', true);

        if ($this->lock !== null) {
            flock($this->handle, LOCK_UN);
        }
        $this->close();

        return $replaced;
    }
406596
protected function remapRecursively(&$data, $oldKey, $newKey)
    {
        $data->loopThrough(function ($index, $value) use ($data, $oldKey, $newKey) {
            $matches = $this->checkRegexRecursively($value, $oldKey, $index);
            foreach ($matches as $match) {
                $this->remap($data, $match[0], $this->getRegexProperty($match, $newKey));
            }
        });
    }
871733
public static function replaceStrict(array $source, array ...$replace) {

        $replacements = [];

        foreach ($replace as $items) {
            $replacements[] = array_intersect_key($items, $source);
        }

        return array_merge($source, ...$replacements);

    }
386570
function replace ($search, $replace)
	{
		ensure('Search argument',  $search,  'is_a_string', __CLASS__, __METHOD__);
		ensure('Replace argument', $replace, 'is_a_string', __CLASS__, __METHOD__);

		$result = str_replace($search, $replace, $this);

		return S($result);
	}
412603
public function replace($search, $replacement) {
        $regexEncoding = mb_regex_encoding();
        mb_regex_encoding($this->encoding);

        // Don't want the args being parsed as regex
        $search = preg_quote($search);
        $replacement = preg_quote($replacement);

        $str = mb_ereg_replace($search, $replacement, $this->str);
        mb_regex_encoding($regexEncoding);

        return self::create($str, $this->encoding);
    }
1345992
public function replace($search, $replace)
    {
        return $this->map(function ($str) use ($search, $replace) {
            return str_replace($search, $replace, $str);
        });
    }
1151600
public static function replacePrefix(string $s, string $search, string $replacement = ''): string
	{
		if (strncmp($s, $search, strlen($search)) === 0) {
			$s = $replacement . substr($s, strlen($search));
		}

		return $s;
	}
1293003
protected static function transformReplacement(array $words, $replace)
    {
        return array_map(function ($value) use ($replace) {
            return str_repeat($replace, mb_strlen($value));
        }, $words);
    }
1226384
public static function replaceLastOccurrence($search, $replace, $subject)
    {
        $pos = strrpos($subject, $search);

        if($pos !== false)
        {
            $subject = static::mb_substr_replace($subject, $replace, $pos, mb_strlen($search));
        }

        return $subject;
    }
892323
function replaceCI($search, $replace, $subject, $count = null) {
		if (!is_array($search)) {
			$slen = mb_strlen($search);
			if (0 === $slen) {
				return $subject;
			}
			$lendif = mb_strlen($replace) - mb_strlen($search);
			$search = mb_strtolower($search);
			$search = preg_quote($search);
			$lstr = mb_strtolower($subject);
			$i = 0;
			$matched = 0;
			/** @var string[] $matches */
			$matches = [];
			while (1 === preg_match('/(.*)'.$search.'/Us',$lstr, $matches)) {
				if ($i === $count ) {
					break;
				}
				$mlen = mb_strlen($matches[0]);
				$lstr = mb_substr($lstr, $mlen);
				$subject =
					substr_replace(
						$subject, $replace, $matched+strlen($matches[1]), $slen
					)
				;
				$matched += $mlen + $lendif;
				$i++;
			}
			return $subject;
		}
		else {
			foreach (array_keys($search) as $k ) {
				if (is_array($replace)) {
					if (array_key_exists($k,$replace)) {
						$subject = $this->replaceCI($search[$k], $replace[$k], $subject, $count);
					}
					else {
						$subject = $this->replaceCI($search[$k], '', $subject, $count);
					}
				} else {
					$subject = $this->replaceCI($search[$k], $replace, $subject, $count);
				}
			}
			return $subject;
		}
	}