简体   繁体   English

php根据值合并数组

[英]php merge arrays based on a value

I am trying to send out emails to users from an array loop. 我正在尝试从数组循环向用户发送电子邮件。 Each email can be present in multiple array elements but, other than email, the rest of the elements are different. 每个电子邮件可以存在于多个数组元素中,但是除电子邮件以外,其余元素都是不同的。

I have this code below which tries to create populate two new arrays--one with just email while the other with other values, such as 'name' etc. What I finally need is an array which has something like: 我在下面的代码中尝试创建填充两个新的数组-一个仅包含电子邮件,而另一个包含其他值,例如'name'等。我最后需要的是一个具有类似内容的数组:

$array_final(
             [0]email=>'whatever@com'=> (name1, name 2),
             [1]email=>"second@.com"=>name3,name4
            );

here are my arrays and the loop: 这是我的数组和循环:

$array_emailedinfo_email = array();//will hold just email
$array_emailedinfo = array();//will hold email and other date; 

foreach($array_expiredmeetingscos as $mail) {

 if(isset($checkAgainst[$mail[1]])) {

    $array_emailedinfo_email[] = array(
                                             "email" => $checkAgainst[$mail[1]]
                                              );
    $array_emailedinfo[] = array( 
                                        "email" => $checkAgainst[$mail[1]], 
                                        "name" => $mail[2]
                                        );
        }
     }

I think I may be able to get the final array in the foreach loop itself but haven't figured that out. 我想我也许可以在foreach循环中获取最终的数组,但还没有弄清楚。 I have tried array_merge() etc but no luck, btw. 我试过array_merge()等,但是没有运气,顺便说一句。

Any idea? 任何想法? Thanks. 谢谢。

Let's have two arrays: 我们有两个数组:

$emails = array(array('email' => 'foo@example.com'), array('email' => 'bar@example.com');
$emails_info = array(array('email' => 'foo@example.com', 'name' => 'Alice'), array('email' => 'bar@example.com', 'name' => 'Bob'));

Now you would like to loop over $emails and find appropriate values in $emails_info : 现在,您想遍历$emails并在$emails_info找到适当的值:

foreach ($emails as $email) {
  $info = array_filter($emails_info, function($item) use ($email) {
    return $item['email'] == $email['email'];
  });

  // because $emails_info[index]['email'] might not be unique, $info is array of such items
  print_r($info);
}

But I think it would be much easier to index one or both arrays with given email, eg 但是我认为用给定的电子邮件索引一个或两个数组会容易得多,例如

//variable from your question
$array_emailedinfo['foo@example.com'] = array(array('email' => 'foo@example.com', 'name' => 'Alice'));

EDIT 编辑

Function which finds appropriate item in array: 在数组中查找适当项目的函数:

function find_in_emails($emails_info, $email) {
    foreach ($emails_info as $email_info) {
        if ($email_info['email'] == $email)
            return $email_info;
    }
    //not found...
    return array();
}
//usage
//...loop...
$info = find_in_emails($emails_info, $email['email']);
//...endloop...

Here is how I fixed it. 这是我固定的方法。 Note in the new code, $emails is the same as $$array_emailedinfo_email and $emails_info is the same as $array_emailedinfo in the original Question, respectively. 注意,在新代码中,$ emails与原始Question中的$$ array_emailedinfo_email相同,而$ emails_info与$ array_emailedinfo相同。

$array_all_expired_info = array();
$i=0;
$temparray = array();
foreach ($emails as $email) {
if (is_array($email)) {
    $array_all_expired_info[$i] = $email;
    foreach($emails_info as $all_info) {
        if (in_array($email["email"],$all_info)) {
            $array_all_expired_info[$i][] = $all_info;
        }
}
}
$i++;

}//for }//对于

The final array is close to what I needed: First element is just the email the rest are arrays inside the respective elements. 最终的数组接近于我所需要的:第一个元素只是电子邮件,其余元素是各个元素内的数组。 So now I can loop through all the elements, grab the emails, then grab corresponding data (which could be multiple, per email) and simply send email. 因此,现在我可以遍历所有元素,获取电子邮件,然后获取相应的数据(每个电子邮件可能是多个),然后简单地发送电子邮件。

This all wasn't easy. 这一切都不容易。 But learned a few things about php arrays in the process. 但是在此过程中了解了有关php数组的一些知识。 Well worth it. 非常值得。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM