Jose Salazar
Jose Salazar

Reputation: 351

PHP combine two arrays in foreach loop

I need guidance on how to combine two arrays in foreach loop. I have a list of checkboxes that will add user to corresponding list.

To grab my checkboxes as an array using :

<input type="checkbox" name="checkbox_list[]" value="Austin Metro">
<input type="checkbox" name="checkbox_list[]" value="Central Austin">
<input type="checkbox" name="checkbox_list[]" value="Georgetown">
...

Here is my loop:

foreach($_POST['checkbox_list'] as $check) {

$myValues = array('Austin Metro','Central Austin','Georgetown','Lake Travis | Westlake','Leander | Cedar Park','Northwest Austin','Round Rock | Pflugerville | Hutto','San Marcos | Buda | Kyle','Southwest Austin','DFW Metro','Frisco','Grapevine | Colleyville | Southlake','McKinney','Plano','Houston Metro','Conroe | Montgomery','Cy-Fair','Katy','Pearland | Friendswood','Spring | Klein','Sugar Land | Missouri City','Tomball | Magnolia','The Woodlands');
$myIds = array('3','4','5','6','7','8','9','10','11','25','12','13','14','15','24','16','17','18','19','20','21','23','22');

 if (isset($check) && array_search($check, $myValues) !== false){
    $index = array_search($check, $myValues);
    $indeed_mail->indeed_wysija_subscribe( $myIds[$index], $email );
 }
}

Currently it is adding user to the right list but is sending one email confirmation per list, instead of merging all subscribed lists in one single email.(in other words if user selects "Austin" & "Houston" lists, it will get one email for "Austin" and one email for "Houston")

Any suggestions on how to fix? Thanks!

Here is function that add subscriber and sends email confirmation:

public function indeed_wysija_subscribe( $listId, $e_mail, $first_name='', $last_name='' ){
        $user_data = array(
                'email' => $e_mail,
                'firstname' => $first_name,
                'lastname' => $last_name);
        $data = array(
                'user' => $user_data,
                'user_list' => array('list_ids' => array($listId))
        );
        $helper = &WYSIJA::get('user', 'helper');
        if($helper->addSubscriber($data)) return 1;
        else return 0;
}

Upvotes: 0

Views: 510

Answers (3)

trincot
trincot

Reputation: 350137

You should define the array with checkbox names/IDs outside of your loop, and use a more appropriate structure for them:

$map = array (
  3 => 'Austin Metro',
  4 => 'Central Austin',
  5 => 'Georgetown',
  6 => 'Lake Travis | Westlake',
  7 => 'Leander | Cedar Park',
  8 => 'Northwest Austin',
  9 => 'Round Rock | Pflugerville | Hutto',
  10 => 'San Marcos | Buda | Kyle',
  11 => 'Southwest Austin',
  25 => 'DFW Metro',
  12 => 'Frisco',
  13 => 'Grapevine | Colleyville | Southlake',
  14 => 'McKinney',
  15 => 'Plano',
  24 => 'Houston Metro',
  16 => 'Conroe | Montgomery',
  17 => 'Cy-Fair',
  18 => 'Katy',
  19 => 'Pearland | Friendswood',
  20 => 'Spring | Klein',
  21 => 'Sugar Land | Missouri City',
  23 => 'Tomball | Magnolia',
  22 => 'The Woodlands',
);

Now you can even remove the loop completely and continue with this code:

// take from map only those entries that have been checked:
$checks = array_intersect($map, $_POST['checkbox_list']);
// pass the IDs of those values to the emailing class:
$indeed_mail->indeed_wysija_subscribe( array_keys($checks), $email );

You just now need to adapt that indeed_wysija_subscribe so it can deal with arrays. There is only one line to change:

            'user_list' => array('list_ids' => $listId)

Notice that array($listId) becomes just $listId because that argument is now already an array.

Upvotes: 1

SH-
SH-

Reputation: 1642

Ignoring the Title of your questions and focusing on the question asked.

Currently it is adding user to the right list but is sending one email confirmation per list, instead of merging all subscribed lists in one single email.

You need to collate the user ids selected, before you pass it into the subscribe function.

Currently your calling the subscribe function, indeed_wysija_subscribe, inside the foreach loop foreach($_POST['checkbox_list'] as $check) {.

You need to move the subscribe call outside the loop and pass in an array of $listId.


This may require you to modify the indeed_wysija_subscribe function to support the array. Or there may be another function in the library.

I am not sure what is part of the library or not.


Also, you can perform the mapping between city name to numeric id in more direct manner.

$myValues = array('Austin Metro','Central Austin','Georgetown');
$myIds = array('3','4','5','6');
$cityToIdMap = array(
    'Austin Metro'=>'3',
    'Central Austin'=>'4',
    'Georgetown'=>'5'
);

$subscriberIds = array();
foreach($_POST['checkbox_list'] as $check) {
    if(!empty($check) && isset($cityToIdMap[$check])){
         $subscriberIds[] = $cityToIdMap[$check];
    }
}

Upvotes: 0

Jimit Shah
Jimit Shah

Reputation: 92

PHP have array_combine function

http://php.net/manual/en/function.array-combine.php

Upvotes: 0

Related Questions