{"id":"706795f9c320d8ea","repo":"laravel/framework","slug":"array-value-for-key-s-must-be-an-integer-s-fo","errorCode":null,"errorMessage":"Array value for key [%s] must be an integer, %s found.","messagePattern":"Array value for key \\[(.+?)\\] must be an integer, (.+?) found\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Collections/Arr.php","lineNumber":648,"sourceCode":"            if ($callback($value, $key)) {\n                return true;\n            }\n        }\n\n        return false;\n    }\n\n    /**\n     * Get an integer item from an array using \"dot\" notation.\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public static function integer(ArrayAccess|array $array, string|int|null $key, ?int $default = null): int\n    {\n        $value = Arr::get($array, $key, $default);\n\n        if (! is_int($value)) {\n            throw new InvalidArgumentException(\n                sprintf('Array value for key [%s] must be an integer, %s found.', $key, gettype($value))\n            );\n        }\n\n        return $value;\n    }\n\n    /**\n     * Determines if an array is associative.\n     *\n     * An array is \"associative\" if it doesn't have sequential numerical keys beginning with zero.\n     *\n     * @param  array  $array\n     * @return ($array is list ? false : true)\n     */\n    public static function isAssoc(array $array)\n    {\n        return ! array_is_list($array);","sourceCodeStart":630,"sourceCodeEnd":666,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Collections/Arr.php#L630-L666","documentation":"Thrown by Arr::integer() when the value resolved via Arr::get() fails is_int(). The accessor does not coerce numeric strings or floats, so '5', 5.0, true, or null all trigger it.","triggerScenarios":"Calling Arr::integer($data, 'count') when the value is the string '5', a float, a bool, an array, or null (missing key with no int default).","commonSituations":"Decoded JSON where counts come through as strings; a config value that is sometimes null; IDs stored as numeric strings.","solutions":["Pass an int default: Arr::integer($data, 'count', 0).","Cast the source: store/normalize with (int) before reading.","Use Arr::get() + (int) cast if numeric-string coercion is desired.","Fix the dot path or schema so the key genuinely contains an int."],"exampleFix":"// before\n$n = Arr::integer($payload, 'pagination.total');\n\n// after\n$n = Arr::integer($payload, 'pagination.total', 0);\n// or coerce\n$n = (int) Arr::get($payload, 'pagination.total', 0);","handlingStrategy":"type-guard","validationCode":"$value = Arr::get($payload, 'pagination.total');\nif (! is_int($value)) {\n    $value = (int) $value;\n}","typeGuard":"function arrayKeyIsInt(array $array, string $key): bool {\n    return is_int(Arr::get($array, $key));\n}","tryCatchPattern":"try {\n    $n = Arr::integer($payload, 'pagination.total', 0);\n} catch (\\InvalidArgumentException $e) {\n    $n = (int) Arr::get($payload, 'pagination.total', 0);\n}","preventionTips":["Arr::integer() does not coerce — cast numeric strings to int before reading.","Pass an int default to Arr::integer().","Decode JSON with JSON_THROW_ON_ERROR and validate counts are ints."],"tags":["collections","arrays","type-mismatch","laravel"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}