3

I have a certain associative array which contains a list of emails. It's given below:

Array
(
    [0] => Array
        (
            [email] => [email protected]
        )

    [1] => Array
        (
            [email] => [email protected]
        )

    [2] => Array
        (
            [email] => [email protected]
        )

    [3] => Array
        (
            [email] => [email protected]
        )

)

It's being generated from the following controller code:

$data['list_of_emails'] = $this->Trainee_requisition->get_list_of_emails($id);

And the corresponding model code from where the get_list_of_emails($id) method is being accessed:

function get_list_of_emails($id){
        $q = $this->db->query("SELECT `email` FROM `requisition_emails` WHERE `req_id` = $id")->result_array();
        return $q;
}

In the Codeigniter documentations, I've found a way of passing a single dimensional array in the $this->mail->to() method of the Email class, but there is no example of an associative array.

My question is, how can I use the above mentioned associative array in the $this->email->to() method?

My main objective is to generate something like this:

$email_list = array('[email protected]', '[email protected]', '[email protected]', '[email protected]');
$this->email->to($email_list);

Edit:

Although Hassaan's answer is the easiest solution and meets my need perfectly, I'd still appreciate answers which can provide a way to retrieve only one column from a query which has more than one column.

For example, if my query is modified like this:

$q = $this->db->query("SELECT rem.`email`, sr.`is_approve` AS approval_status FROM `requisition_emails` rem INNER JOIN `staff_requisitions` sr ON sr.`id` = rem.`req_id` WHERE rem.`req_id` = $id")->result_array();

but I want only email column to be used from the list, then what is the best possible way?

Edit - 2:

Forgot to mention. PHP version is 5.3.

2
  • 1
    This is comment after you edited the question. In order to retreive only one column of an array comming from your model, use array_column function, which I used in my answer. Commented Aug 24, 2015 at 10:10
  • Ah man finally :) Look at updated answer! Commented Aug 24, 2015 at 12:21

5 Answers 5

3

You need to use foreach loop in your view.

Try Example

foreach($list_of_emails as $email)
{
    echo $email."<br />";
    // You can call '$this->mail->to()' to mail.
}
Sign up to request clarification or add additional context in comments.

3 Comments

I've already used your method, but mail is only sent to the last id, i.e. [email protected] and not to the others.
Thanks mate, it's working. The problem was in my further code, I used a die(). That's why, email was always being sent to only one address. Also, I had to put the $this->email->send() inside the foreach().
Mate, is it possible for you to update your answer? I have updated my question and looking for yet another small enhancement.
2

As per CI documentation, you can send email to multiple recipients using the following construction:

$this->email->to('[email protected], [email protected], [email protected]');

In order to generate this kind of string, do following steps:

Don't touch your model

Use PHP implode and array_column functions to extract email field and to concat all array elements by comma:

$data['list_of_emails']=$this->Trainee_requisition->get_list_of_emails($id);//Get data from model as one dimensional string
$email_list = implode(',',array_column($data['list_of_emails'],'email'));//Implode array elements with comma as separator
$this->email->to($email_list);

You can print $email_list to ensure you have right format by calling var_dump($email_list); exit;.

EDIT (If you are using PHP < 5.5)

Use array_map function, and make the first parameter as anyonimus function. By defining $columnName, you can extract any column you want.

 $columnName = "email";
$result = array_map(
    function ($arrayRow) use ($columnName){
        return $arrayRow[$columnName];
    }
,$data['list_of_emails']);
var_dump(implode(",",$result));
$this->email->to($result );

4 Comments

In my question, for convenience I've fetched only one column in my query. What if I re-write my query so that it returns more than one column? How can I fetch email then?
Update is there, please check and try!
I'm testing. I'll let you now later. Thanks!
array_column() doesn't work for PHP versions earlier than 5.5 . Mine is 5.3.
1

You can simply array_column as

$new_array = array_column('email');

Comments

1

this should work!

<?php  $array = array(array("email" => "[email protected]"), array("email" => "[email protected]")); $emailList = array_map('current', $array); print_r($emailList);

Comments

0

Try this may be solve your problem.

$query->result_array();

it will gives single dimensional array

1 Comment

I think you didn't see my question properly. I've used result_array() method in my model code.

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.