Title: How do I get data from a nested array returned by $wpdb?
Last modified: August 30, 2016

---

# How do I get data from a nested array returned by $wpdb?

 *  [RedChill](https://wordpress.org/support/users/redchill/)
 * (@redchill)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/how-do-i-get-data-from-a-nested-array-returned-by-wpdb/)
 * Hi all, so I’m trying to do something like this…
 * $theResult=array();
    $theUserNames=array();
 * $results = $wpdb->get_results(“SELECT * FROM firstTable WHERE user_num=16”);
 * foreach($results as $row){
 *  $theResult[]=$row->id;
 * }
 *  foreach($theResult as $newId)
    {
 * $userNames = $wpdb->get_results($wpdb->prepare(“SELECT userInfo FROM secontTable
   WHERE ID=%d LIMIT 1”,$newId));
 * $theUserNames[]=$userNames;
    } print_r($theUserNames);
 * This seems to get the data but I have trouble unwrapping it. The result looks
   something like this:
 * Array ( [0] => Array ( [0] => stdClass Object ( [userInfo] => theDataThatIWant))).
 * How do I get just theDataThatIWant and put them in an unnested array?

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Christian Chung](https://wordpress.org/support/users/christian1012/)
 * (@christian1012)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/how-do-i-get-data-from-a-nested-array-returned-by-wpdb/#post-6405337)
 * You can control how the output of `$wpdb` methods is formatted. Options are:
 * OBJECT – result will be output as a numerically indexed array of row objects.
   
   OBJECT_K – result will be output as an associative array of row objects, using
   first column’s values as keys (duplicates will be discarded). ARRAY_A – result
   will be output as a numerically indexed array of associative arrays, using column
   names as keys. ARRAY_N – result will be output as a numerically indexed array
   of numerically indexed arrays.
 * More at [https://codex.wordpress.org/Class_Reference/wpdb](https://codex.wordpress.org/Class_Reference/wpdb)
 *  Thread Starter [RedChill](https://wordpress.org/support/users/redchill/)
 * (@redchill)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/how-do-i-get-data-from-a-nested-array-returned-by-wpdb/#post-6405423)
 * Thanks, that without the ‘prepare’ statement. When I added the ‘prepare’ to it
   I It did not give me the associate array. Here’s what I tried.
 * $userNames = $wpdb->get_row($wpdb->prepare(“SELECT userInfo FROM secontTable 
   WHERE ID=%d “,$newId,ARRAY_A));
 * How can I use the best practice with the ARRAY_A?
 * Thanks again for helping

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How do I get data from a nested array returned by $wpdb?’ is closed to
new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [RedChill](https://wordpress.org/support/users/redchill/)
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/how-do-i-get-data-from-a-nested-array-returned-by-wpdb/#post-6405423)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
