json_decode() expects parameter 1 to be string, array given?

json_decode() expects parameter 1 to be string, array given?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

json_decode() expects parameter 1 to be string, array given?

You are encountering this error because you are attempting to pass an already-decoded PHP array into a function that expects a serialized JSON string. This is a classic logic error in data persistence workflows: the variable has been transformed once from its original state, and your code assumes it remains in that initial state without verifying its current type.

When you look at the source material provided, the data structure is shown as a PHP Array. Specifically, it contains a single key (likely an empty string or an index) holding an array of associative arrays representing transaction records. This output format is exactly what PHP's var_dump() or print_r() generates when inspecting a variable in memory or within a debugging tool like Xdebug.

The error message json_decode() expects parameter 1 to be string, array given is explicit. The function signature for json_decode() is defined to accept only a string containing valid JSON data. If you pass an array directly to it, PHP throws a fatal error immediately because the internal parsing logic cannot operate on native PHP types in that context.

To resolve this, you must ensure that the variable passed to json_decode() is actually a string before attempting the operation. The most robust way to handle this check in modern PHP versions (7.4 and later) is using the is_string() function combined with a type declaration or strict error handling.

Here is how you can conditionally decode data:

$data = $yourVariable; // This could be a string or an array depending on context

if (is_string($data)) {
    $decoded = json_decode($data, true);
    
    if (json_last_error() !== JSON_ERROR_NONE) {
        throw new InvalidArgumentException('Invalid JSON provided');
    }
} else {
    // The data is already an array; no decoding needed.
    $decoded = $data;
}

In many scenarios, particularly when retrieving data from a database or session storage, the data might be stored as a serialized string (using json_encode() previously) but retrieved as a raw string. However, if you are iterating through an application flow where data is passed between functions, it is highly probable that at some point, the result of a previous json_decode() call was assigned to the variable without being re-serialized back into a string.

If your goal is to persist this specific array structure shown in the source material to a file or database for later retrieval, you must use json_encode() on the array first. The resulting output will be a JSON string. When you read that string back from the storage medium, you can safely pass it to json_decode().

$arrayData = [
    ['customerId' => '1', 'customer_name' => 'Jon_doe', // ... other fields]
];

// Convert array to JSON string for storage
$jsonString = json_encode($arrayData); 

// Later, when reading from storage:
if (is_string($retrievedData)) {
    $arrayData = json_decode($retrievedData, true);
}

There is a specific edge case worth noting regarding the source material's output format. The snippet shows an array containing another array: Array ( [0] => Array(...) ). If you are manually constructing this structure or reading it from a file, ensure that the outer wrapper is handled correctly. JSON does not support multi-dimensional arrays in the same way PHP does; it supports nested objects and arrays, but the serialization must be complete.

If you are using a framework like Laravel, the json_encode() function behaves identically to the core PHP function, but you might also utilize collect() or Carbon if you need to manipulate the dates (txdate, txtime) found in the transaction data before encoding. However, the fundamental requirement remains unchanged: never pass a native PHP array directly into json_decode().

If you find yourself constantly debugging this issue, consider implementing a helper method on your model or repository class that normalizes the input. This ensures that regardless of whether the data arrives as a string from an API response or is being reconstructed from local state, it is always treated consistently before any JSON manipulation occurs. Consistency in data types across your application boundaries is key to preventing these runtime errors.

Our network