$value) { if (!array_key_exists($field, $initialArray)) { $changes[$field] = $value; } elseif ($recursive && is_array($initialArray[$field]) && is_array($value)) { $newVal = $this->getChanges($initialArray[$field], $value, $recursive, $callback); if (!empty($newVal)) { $changes[$field] = $newVal; } } elseif ($callback === null && $value !== $initialArray[$field]) { $changes[$field] = $value; } elseif ($callback !== null && !$callback($value, $initialArray[$field])) { $changes[$field] = $value; } } return $changes; } /** * If the given $key is set in the array and if the corresponding value is not null, * cast this value and returns it. Else, returns null. * * Ex: * * $array = ['a' => '123'] * * ArrayUtils::getAndCast($array, 'a', 'int') // => returns `123` as an integer * ArrayUtils::getAndCast($array, 'b', 'int') // => returns `null` * * @param mixed[] $array * @param string $key * @param string $type * @return mixed */ public static function getAndCast(array $array, mixed $key, string $type): mixed { $value = $array[$key] ?? null; if ($value !== null) { settype($value, $type); } return $value; } }