Jump to content
Larry Ullman's Book Forums

Convert Standard Class Object


Recommended Posts

How do you convert an array of objects to a simple array? For example, I have

$emails = Array ( [0] => stdClass Object ( [email] => example@email.com) [1] => stdClass Object ( [email] => example2@email.com ) [2] => stdClass Object ( [email] => example3@email.com ) )

and I'd like it be

Array ( [0] => example@email.com [1] => example2@email.com [2]=> example3@email.com )

 

I've tried using a loop to cast the object to an array but that didnt seem to work. Thanks for any suggestions.

Link to comment
Share on other sites

I'm finshing off a website for someone whose original developer has gone awol. Its a wordpress site and the functionality in question uses the wpdb class to access a custom table and do different things depending on what's returned by the query.

$emails = $wpdb->get_results( "SELECT email FROM emails WHERE zip_prefix = '$zip_prefix'" );
foreach ($rows as $obj) {
$email_array2[] = $obj->email;
print_r($emails);
print_r($email_array2);

return $email_array2;

The above code is inside a function which passes the email array to phpmailer.php. I need to format the data so phpmailer.php can use it. Everything seems to be working other than this step.

 

The 2 print_r statements are so I can see what is going on and will be deleted when all is working. There is definitely more than 1 object in the array returned by the wpdb class. The output from print_r($emails) is

Array ( [0] => stdClass Object ( [email] => example2@email.com ) [1] => stdClass Object ( [email] => example3@email.com ) [2] => stdClass Object ( [email] => example@email.com ) )

and the output from print_r(email_array2) is

Array ( [0] => example2@email.com )

 

I thought I had to use the wpdb class to query the table. If there is another way, please let me know.

 

Thanks for all the suggestions.

Link to comment
Share on other sites

$emails = $wpdb->get_results( "SELECT email FROM emails WHERE zip_prefix = '$zip_prefix'" );
foreach ($rows as $obj) {
$email_array2[] = $obj->email;
print_r($emails);
print_r($email_array2);

return $email_array2;

 

I'm struggling to understand the code, does $emails turn into $rows at some point and that code is missing?

 

Where's the closing curly bracket for the foreach loop? If it's after the return $email_array2, that could be affecting your loop.

 

Is it possible to provide more of the script?

 

You can return each row as an array (associative or numeric) by passing in a second parameter (output_type) to get_results:

 

ARRAY_A

ARRAY_N

 

$wpdb->get_results('query', output_type);

 

http://codex.wordpre...Generic_Results

 

Which may be more helpful to you.

Link to comment
Share on other sites

Sorry rob - my bad. Posting code on the run is not a good idea! And you are right, the curly bracket was in the wrong place. Thanks alot for the help.

 

This project is pushing the boundaries of my experience with oop so I may be back with more questions.

 

Final code that works is

$emails = $wpdb->get_results( "SELECT email FROM emails WHERE zip_prefix = '$zip_prefix'" );
foreach ($emails as $obj) {
$email_array2[] = $obj->email;
}
return $email_array2;

Link to comment
Share on other sites

 Share

×
×
  • Create New...