margaux Posted September 21, 2012 Share Posted September 21, 2012 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 More sharing options...
rob Posted September 21, 2012 Share Posted September 21, 2012 foreach($emails as $obj) { $new_array[] = $obj->email; } Link to comment Share on other sites More sharing options...
margaux Posted September 21, 2012 Author Share Posted September 21, 2012 Thanks rob! One thing though - I'm only getting the first object put into the array. Any ideas? Link to comment Share on other sites More sharing options...
HartleySan Posted September 21, 2012 Share Posted September 21, 2012 How about a foreach loop inside of a foreach loop? Link to comment Share on other sites More sharing options...
rob Posted September 21, 2012 Share Posted September 21, 2012 Thanks rob! One thing though - I'm only getting the first object put into the array. Any ideas? You're sure you have more than one object in your $emails array? Otherwise, will have to see some code. Link to comment Share on other sites More sharing options...
Larry Posted September 21, 2012 Share Posted September 21, 2012 If that's your need, I would instead see if it's not possible to fetch the email addresses NOT as objects from the get-go. Link to comment Share on other sites More sharing options...
margaux Posted September 21, 2012 Author Share Posted September 21, 2012 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 More sharing options...
rob Posted September 21, 2012 Share Posted September 21, 2012 $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 More sharing options...
margaux Posted September 21, 2012 Author Share Posted September 21, 2012 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 More sharing options...
rob Posted September 21, 2012 Share Posted September 21, 2012 No worries, you got it working which is the main thing. Link to comment Share on other sites More sharing options...
Recommended Posts