Warning: explode() expects parameter 2 to be string, array given

Warning: explode() expects parameter 2 to be string, array given

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

Stefan Bogdanescu

Founder & Senior Architect

Why explode() Fails When Your Variable Holds an Array

The error message Warning: explode() expects parameter 2 to be string, array given is a classic PHP runtime warning that signals a fundamental misunderstanding of data types. In the specific scenario you described, the issue stems from how the variable $thearray is constructed versus what the explode() function actually requires.

The Data Flow Problem

Let's trace the execution path in your script. You execute a SQL query against table_two and fetch a single row using mysqli_fetch_assoc(). Inside that row, the column some_text_id contains the value 164, 165, 166.

You correctly assign this string value to an array:

$thearray[] = $row['some_text_id'];
// Result: ['164, 165, 166']

At this exact moment, $thearray is an array containing one element. That single element happens to be a string representing a comma-separated list of numbers.

However, your code immediately follows up with print_r($thearray). This outputs Array ( [0] => 164, 165, 166 ). While this might look like the raw data, it confirms that $thearray is technically an array object.

The fatal mistake occurs on the next line:

$thearray = explode(", ", $thearray);

The explode() function signature in PHP is explode(string $delimiter, string $string). It strictly expects the second argument to be a string. By passing $thearray (which is an array) as that second argument, you are telling PHP to "split this array by commas." Since the first element of your array is already a string, PHP sees the array container itself and throws the type mismatch warning.

The Correct Approach: Unwrapping First

To fix this, you must isolate the actual string data before passing it to explode(). You need to access the first (and currently only) element of $thearray by using index [0].

Here is the corrected logic:

// Fetch the row as before
$row = mysqli_fetch_assoc($tqr);

// Create an array holding the single string
$thearray[] = $row['some_text_id']; 

// Extract the actual string value from the array
$rawString = reset($thearray); // Or simply: $rawString = $thearray[0];

// Now explode works correctly because parameter 2 is a string
$myArray = explode(", ", $rawString);

print_r($myArray);

After running this, $myArray will contain three distinct elements: ['164', '165', '166']. You can now iterate over them or access specific indices like $myArray[0] without type errors.

A Better Pattern for Database Queries

While fixing the immediate error is straightforward, relying on a single-row fetch to build an array often indicates a design pattern that might be brittle. If your business logic requires selecting multiple IDs based on a comma-separated list stored in a column, consider these architectural alternatives:

  1. Normalization: Ideally, some_text_id should store individual integers in separate rows rather than one giant string per row. This allows you to use standard SQL IN clauses like SELECT * FROM table_two WHERE id IN (164, 165, 166).
  2. Dynamic SQL Construction: If the data must remain comma-separated strings, construct your query dynamically based on the split array. Be extremely careful here to prevent SQL injection; never interpolate raw user input directly into a query string.

Handling Edge Cases

Before splitting the string, always validate that the delimiter actually exists or that the string is not empty. If $row['some_text_id'] is null or missing, explode() might return unexpected results depending on how you handle the subsequent logic.

Additionally, note that explode() returns an array of strings. If your downstream logic expects integers (e.g., for arithmetic operations), you will need to cast them:

$id = (int) $myArray[0];

In summary, the error arises because you are passing a container (array) where PHP expects content (string). Unwrapping the value with $thearray[0] resolves the immediate runtime warning and allows the string manipulation to proceed as intended.

Our network