Home > Net >  Implode() not working with associative array Wordpress
Implode() not working with associative array Wordpress

Time:09-28

I would like to send email on custom post update, for which I saved email in a different table 'wp_offers', there are multiple entries for every single post.

What I am trying to is using implode() function to send email on all stored email address, but implode is not working:

function send_emails_on_new_event( $post ) {

$id = get_the_ID( $post->ID );

 global $wpdb;
    // this adds the prefix which is set by the user upon instillation of wordpress
    $table_name = $wpdb->prefix . "offers";
    // this will get the data from your table
    $retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" . "WHERE post_id=" . $id);
foreach ($retrieve_data as $retrieved_data){ 
    $email = implode(",",$retrieved_data->email);
}



    $emails  = $email;
    $headers = 'From: Name <[email protected]>';
    $title   = wp_strip_all_tags( get_the_title( $post->ID ) );
    $message = 'New post created' . " " . $email;

    if ( get_post_type( $post->ID ) === 'property' ) {
        wp_mail( $emails, 'New Post', $message, $headers );
    }
}
add_action( 'pre_post_update', 'send_emails_on_new_event' );

I displayed all emails using this code but I want them one by one separated with comma ,

This code worked and displayed all emails in <li> :

 <?php
    
        $id = $post->ID;
    
     global $wpdb;
        // this adds the prefix which is set by the user upon instillation of wordpress
        $table_name = $wpdb->prefix . "offers";
        // this will get the data from your table
        $retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
    ?>
    <ul>
       <?php foreach ($retrieve_data as $retrieved_data){ ?>
           <li> <?php echo $retrieved_data->email;?></li>
           
        <?php 
            }
        ?>
    </ul>

CodePudding user response:

You do this:

foreach ( $retrieve_data as $retrieved_data ){ 
    $email = implode( ",", $retrieved_data->email );
}

It doesn't work because you're giving implode() a string, not an arry.

Try making an array of the email strings, then imploding that.

$emails = array();
foreach ( $retrieve_data as $retrieved_data ){
    $emails[] = $retrieved_data->email;
}
$email = implode( ",", $emails );

This is a common code pattern for turning some column from a SQL resultset into a comma-separated list.

CodePudding user response:

The problem is that implode expects array

 implode(string $separator, array $array): string

What you could do is to collect the emails in one array and then implode. Something like this might help:

$emailsData = [];
foreach ($retrieve_data as $retrieved_data){
    $emailsData[] = $retrieved_data->email;
} // foreach
$emails = implode(",",$emailsData);

CodePudding user response:

The confusion lies in wanting to implode something that is not an array:

foreach ($retrieve_data as $retrieved_data)

By the way these are too generic for variable names: they do not describe the data at all. Unless the object you are building is very generic, this is poor practice. I will rename them so that the answer begins to become clear. I am assuming this is a user object, but it can be anything else:

foreach ($user_object_array as $user_object){
// a $user_object_array has multiple user objects
// a $user_object has all the different properties
$email_array[] = $user_object->email;
// push a new piece of data into a flat email array
}
$emails_string = implode(", ", $email_array);
echo $emails_string;

The implode() fails because the argument for the implode is not an associative array, but a simple string $retrieved_data->email.

Additionally, if you want to use the wp_mail function, you do not need to implode it if you already have it in an array: https://developer.wordpress.org/reference/functions/wp_mail/

wp_mail( string|string[] $to, string $subject, string $message, string|string[] $headers = '', string|string[] $attachments = array() )

Parameters

$to (string|string[]) (Required) Array or comma-separated list of email addresses to send message.

  • Related