• 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)
  • 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.ww.wp.xz.cn/Class_Reference/wpdb

    Thread Starter RedChill

    (@redchill)

    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.