Convert stdClass object to array in PHP

Convert stdClass object to array in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Convert stdClass object to array in PHP

When querying data directly from the WordPress database using $wpdb, you often encounter a specific pattern: get_results() returns an array of stdClass objects, even when your SQL query selects only scalar values like an ID. In the scenario provided, the goal is to extract a simple indexed array of integers—such as [140, 141, 142]—instead of navigating through nested object properties.

The root cause lies in how MySQL's result sets are mapped to PHP variables by default. When $wpdb->get_results() executes a query and finds multiple rows, it creates an array where each element represents a row as an object. Even if your SQL statement is SELECT post_id FROM ..., the resulting object retains that column name as a property ($row->post_id). To transform this into the flat integer array you need, you must extract the specific property from each object and rebuild the structure.

The most direct approach uses a simple loop to iterate over the result set and collect the values into a new array:

$post_id = $wpdb->get_results("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'mfn-post-link1' AND meta_value = '". $from ."'", ARRAY_N);

// If you cannot change the query flags or need to handle existing stdClass objects:
$ids = array_map(function($row) {
    return (int)$row->post_id;
}, $post_id);

There are two distinct strategies here depending on how you structure your SQL call. The first strategy leverages the third argument of get_results(). By passing ARRAY_N as the flag, WordPress skips the default object creation and returns an array of arrays instead. This means $post_id[0] is already an array like [ 'post_id' => 140 ], allowing you to grab the value immediately with $post_id[$i]['post_id']. This method is generally preferred for performance because it avoids the overhead of instantiating objects when you don't need them.

If you are constrained by existing code or require an older version of WordPress where ARRAY_N isn't viable, the array_map approach shown above is robust. It explicitly converts each stdClass instance to its integer value. The type casting (int) is crucial here; it ensures that any potential whitespace or string formatting in the database returns a clean integer, preventing issues with arithmetic operations later.

Why not use json_decode?

A common misconception is using json_encode() followed by json_decode(..., true) to flatten objects. While this technically works, it adds unnecessary serialization overhead. Converting an object to JSON string and back to an array forces the CPU to serialize and deserialize data that was already in memory. For high-performance applications or large datasets, direct property extraction via loops or array_map is significantly faster.

Handling the Data Structure

Once you have converted the objects into a flat array of integers, your subsequent logic becomes much simpler. You can pass this directly to functions expecting an ID list, use it in in_array() checks, or feed it into a second query using $wpdb->get_col().

// $ids is now: [140, 141, 142]
foreach ($ids as $id) {
    // Direct integer usage
    echo "Processing post {$id}<br>"; 
}

Security Considerations

While the article focuses on data transformation, it is vital to note that the SQL query in your source material uses string concatenation for the meta_value parameter: WHERE meta_value = '". $from ."'". This is a classic vulnerability vector. If $from contains user input, you are susceptible to SQL injection attacks. Regardless of how you parse the result set, ensure that $from is sanitized or prepared using $wpdb->prepare() before being inserted into the query string.

// Safer implementation
$prepared_from = $wpdb->esc_like($from); 
$sql = "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'mfn-post-link1' AND meta_value = %s";
$post_id = $wpdb->get_results($sql, ARRAY_N, array($prepared_from));

By switching to ARRAY_N and sanitizing inputs, you achieve both the desired data structure and a more secure codebase.

Our network