1

Please i need assistance in converting objects that is returned from $wpdb->get_results into arrays so that i can call them values like $query['username'] instead of using $Query->username

because my method of doing it is not flexible enough and it makes me write many codes by manually creating those column as array just like this code below
$data = array('username = $query->username, email = $query->email');
but i believe there is a simple way to do this automatically without having to retype all column names one by one

3 Answers 3

1

$wpdb->get_results has a second parameter that lets you specify what kind of return value you want:

For example:

$data = $wpdb->get_results( $query, ARRAY_A );

Here you get an associative array back.

6
  • i don't seems to get how using ARRAY_A works as it throws errors maybe am not using it rightly, can you write the code in full for better understanding of its usage Commented Jun 3, 2020 at 10:25
  • @pandglobalWhat error do you get, and what code exactly are you using? Just update your question, then let me know in a comment here. Commented Jun 3, 2020 at 10:31
  • $data = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}users", ARRAY_A ); echo $data[0]['username'] this works too but i always end up with $date[0]['username'] but what i wanted was a simply $data['username'] not array of arrays Commented Jun 3, 2020 at 10:36
  • $data = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}users", ARRAY_A ); echo $data[0]['username'] this works too but i always end up with $date[0]['username'] but what i wanted was a simply $data['username'] not array of arrays Commented Jun 3, 2020 at 10:36
  • Well, you get all users, so each user is an entry in the array, and each user's data is a sub-array. If you want only one user, add LIMIT 1 to your query. Commented Jun 3, 2020 at 10:45
1
global $wpdb;
$data =  $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}users", ARRAY_A );

foreach($data as $user){ 
$deuser = $user;
}

echo $deuser['username'];`
5
  • This was what i arrived at from getting the suggestions (@)Baikaree and @fuxia gave so i now can echo any of the column values like this $deuser['username'] without having to write it like this $data[0]['username'] Commented Jun 3, 2020 at 11:20
  • If you only want one row, use $wpdb->get_row(), and add LIMIT 1 like fuxia suggested. Commented Jun 3, 2020 at 11:40
  • @jacob Peattie if i use LIMIT or the WHERE username = 'john' clause to get a single row as you suggested without using foreach function the way i did i can't still echo the results like this $data['username'] this is something i have the or reason was that am using a custom table that columns username, email, phone but was using the $wpdp to query my custom table, i guess i should have changed the table name from users to my customers, for clarity, but however am glad to have it working for me using my answer above. Bcs wordpress users table has a different structure. Commented Jun 3, 2020 at 12:44
  • @pandglobal get_row() also supports the 2nd parameter just as get_results(), so you can do $row = $wpdb->get_row( "YOUR QUERY", ARRAY_A ); echo $row['column_name'];. And your code as it is in your answer is just inefficient - you're requesting all rows just to get the last in the results? Commented Jun 3, 2020 at 15:35
  • @sally Cj yes you maybe right because i wasn't thinking about the technical aspect of databases query, however i was thinking that by me using the WHERE clause will help me only return one row with many columns which is what happened, but i don't know much about querying optimization so i will look up the get_row() and see it that helps, but if it has no impact on my database i just leave my code the it is working for me Commented Jun 3, 2020 at 19:23
0

Hey you can try this to use the Object in array format:

global $wpdb;
$data =  $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}users", OBJECT );
$data = json_decode( json_encode($data), true );
echo $data[0]['user_email'];

Also, you can use the unset() method to remove unwanted columns from the array. By using this way you can achieve.

6
  • Thanks @Baikare Sandeep tbe Commented Jun 3, 2020 at 10:18
  • Thanks @pandglobal this solutions helps you. Please mark as correct if it resolves. Commented Jun 3, 2020 at 10:21
  • @Biakare sandeep your suggests worked, but was just thinking using $query->username or using $query[0]['username'] which one is more complex, Commented Jun 3, 2020 at 10:22
  • Yes using object is more easier than array. But I think you had scenario to use array in your question. Commented Jun 3, 2020 at 10:25
  • Yes you are right there are some good futures in using array such as array merge, array_combine so on which will help me alot in the project and make coding simplier Commented Jun 3, 2020 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.