Message: Trying to access array offset on value of type int

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Message: Trying to access array offset on value of type int – Mastering Defensive Programming in PHP As senior developers, we often encounter frustrating runtime errors that seem simple but hide complex underlying data integrity issues. One of the most common and irritating errors in PHP is: **"Trying to access array offset on value of type int."** This error doesn't signal a syntax mistake; instead, it signals a fundamental mismatch between what your code *expects* (an array) and what it *receives* (a simple integer). Understanding this error is the first step toward writing robust, production-ready code. In this post, we will dissect why this error occurs in the context of handling request data, explore the root causes, and provide concrete solutions using best practices, especially when working within a framework like Laravel. --- ## Understanding the Error: The Type Mismatch The core issue lies in attempting to use array access syntax (like `$variable['key']`) on a variable that holds a primitive type, specifically an integer (`int`), instead of an array (`array`). Let's look at the problematic context provided: ```php $orders = $request->get('order'); // Assume this retrieves data // ... inside loop if($order['id']) // Error likely occurs here if $order is an int, or if $order['id'] fails subsequent checks { $ordersArray[$order['id']]['order_id'] = $order['id']; // Attempting to use the result as an index // ... } ``` If `$request->get('order')` returns a single integer (e.g., if the request parameter is `?order=123`), then `$orders` becomes `123` (an `int`). When the loop attempts to execute `$order['id']`, PHP throws the error because you cannot access an array offset on an integer. ## The Developer Solution: Defensive Coding with Type Checking The solution is simple yet powerful: **always validate the type of a variable before attempting to use it as an array.** This practice, known as defensive coding, prevents your application from crashing when dealing with unpredictable user input or API responses. ### Step 1: Check if the Variable Exists and is an Array Before iterating over or accessing elements in `$orders`, we must confirm that `$orders` is indeed an array. If it is not, we should skip the operation or handle the error gracefully. ### Step 2: Refactoring the Code for Robustness We can refactor the provided snippet to safely handle cases where `$orders` might be an integer, `null`, or a non-array structure. This approach aligns perfectly with the principles of building secure and reliable APIs, much like structuring data within Laravel components. Here is the corrected and robust implementation: ```php public function processPayment(Request $request) { $orders = $request->get('order'); $ordersArray = []; // 1. Check if $orders exists AND is an array before looping if (is_array($orders)) { // getting orders details foreach ($orders as $order) { // 2. Inside the loop, ensure $order itself is also an array and has necessary keys if (is_array($order) && isset($order['id']) && isset($order['quantity'])) { $ordersArray[$order['id']]['order_id'] = $order['id']; $ordersArray[$order['id']]['quantity'] = $order['quantity']; } else { // Optional: Log or handle malformed individual records \Log::warning("Skipping malformed order record found: " . json_encode($order)); } } } else { // Handle the case where the input was not an array (e.g., it was a single ID) \Log::error("Input 'order' was received, but it was not an array."); } dd($ordersArray); } ``` ## Best Practices: Beyond Simple Checks While checking `is_array()` solves the immediate crash, robust development requires more than just catching exceptions. When dealing with data flowing through your application—whether it’s from a controller receiving a request (like in Laravel) or from an external API—you must implement strict validation. 1. **Use Framework Validation:** In a Laravel environment, always leverage the built-in validation system (`$request->validate([...])`). This handles type checking and missing fields immediately before your business logic even executes, saving you manual checks inside every method. 2. **Type Hinting (PHP 8+):** Utilizing strict type hints (as seen in modern PHP) helps the interpreter catch potential type errors earlier during development, reducing runtime surprises. 3. **Data Contracts:** Treat all incoming data as untrusted. Define clear "data contracts" for what structure you expect from external sources and enforce those contracts rigorously on the server side. ## Conclusion The error "Trying to access array offset on value of type int" is a classic reminder that defensive programming is non-negotiable in software development. By proactively checking variable types (`is_array()`) before attempting operations, we build systems that are resilient to unexpected input, which is crucial when managing data from HTTP requests and external services. By integrating these checks into your data processing pipeline, you ensure that your application remains stable, predictable, and professional, adhering to the high standards expected in modern PHP frameworks like Laravel.