如何合并具有相同键'email'的数组。像这样的例子[4]和[6]有相同的电子邮件地址。
Array
(
[0] => Array
(
[email] => test01@laposte.net
[gender] => m
[lastname] => Canvas
[firstname] => Peter
[birthdate] => 1987-09-13
[country] => DE
[xgfx3r] =>
[language] => fr_FR
[newsletter] => 0
[newsletter_partner] => 0
[facebook_login] => yes
[date] => 2011-08-11 15:41:54
[vol6t3] => L'homme de bernadette
[uwwpma] =>
[bhqgzn] =>
[nb_email_shared] =>
[period] => 1
)
[1] => Array
(
[email] => test@laposte.net
[gender] => m
[lastname] => Canvas
[firstname] => Peter
[birthdate] => 1987-09-13
[country] => DZ
[xgfx3r] =>
[language] => fr_FR
[newsletter] => 0
[newsletter_partner] => 0
[facebook_login] => yes
[date] => 2011-08-11 15:42:50
[vol6t3] => L'homme de bernadette
[uwwpma] =>
[bhqgzn] =>
[nb_email_shared] =>
[period] => 1
)
[2] => Array
(
[email] => sqd@test.com
[gender] => m
[lastname] => qsd
[firstname] => qsd
[birthdate] => 1981-01-17
[country] => AT
[xgfx3r] =>
[language] => fr_FR
[newsletter] => 0
[newsletter_partner] => 0
[facebook_login] =>
[date] => 2011-08-11 15:43:22
[vol6t3] => Un militant de gauche
[uwwpma] =>
[bhqgzn] =>
[nb_email_shared] =>
[period] => 1
)
[3] => Array
(
[email] => qsd@laposte.net
[gender] => m
[lastname] => qsd
[firstname] => qsdqsd
[birthdate] => 1982-02-19
[country] => BS
[xgfx3r] =>
[language] => fr_FR
[newsletter] => 0
[newsletter_partner] => 0
[facebook_login] =>
[date] => 2011-08-11 15:45:13
[vol6t3] => Un militant de gauche
[uwwpma] =>
[bhqgzn] =>
[nb_email_shared] =>
[period] => 1
)
[4] => Array
(
[email] => qsdqs@laposte.net
[gender] => m
[lastname] => qds
[firstname] => dqs
[birthdate] => 1980-11-19
[country] => BS
[xgfx3r] =>
[language] => fr_FR
[newsletter] => 0
[newsletter_partner] => 0
[facebook_login] =>
[date] => 2011-08-11 15:46:14
[vol6t3] => Un militant de gauche
[uwwpma] =>
[bhqgzn] =>
[nb_email_shared] =>
[period] => 1
)
[5] => Array
(
[email] => lol@laposte.net
[gender] => m
[lastname] => Canvas
[firstname] => Peter
[birthdate] => 1987-09-13
[country] => DE
[xgfx3r] =>
[language] => fr_FR
[newsletter] => 0
[newsletter_partner] => 0
[facebook_login] => yes
[date] => 2011-08-11 15:47:11
[vol6t3] => L'homme de bernadette
[uwwpma] =>
[bhqgzn] =>
[nb_email_shared] =>
[period] => 1
)
[6] => Array
(
[email] => qsdqs@laposte.net
[gender] => m
[lastname] => Canvas
[firstname] => Peter
[birthdate] => 1987-09-13
[country] => DE
[xgfx3r] =>
[language] => fr_FR
[newsletter] => 0
[newsletter_partner] => 0
[facebook_login] => yes
[date] => 2011-08-11 15:48:35
[vol6t3] =>
[uwwpma] => Toi
[bhqgzn] =>
[nb_email_shared] =>
[period] => 2
)
)
到
Array
(
[0] => Array
(
[email] => test01@laposte.net
[gender] => m
[lastname] => Canvas
[firstname] => Peter
[birthdate] => 1987-09-13
[country] => DE
[xgfx3r] =>
[language] => fr_FR
[newsletter] => 0
[newsletter_partner] => 0
[facebook_login] => yes
[date] => 2011-08-11 15:41:54
[vol6t3] => L'homme de bernadette
[uwwpma] =>
[bhqgzn] =>
[nb_email_shared] =>
[period] => 1
)
[1] => Array
(
[email] => test@laposte.net
[gender] => m
[lastname] => Canvas
[firstname] => Peter
[birthdate] => 1987-09-13
[country] => DZ
[xgfx3r] =>
[language] => fr_FR
[newsletter] => 0
[newsletter_partner] => 0
[facebook_login] => yes
[date] => 2011-08-11 15:42:50
[vol6t3] => L'homme de bernadette
[uwwpma] =>
[bhqgzn] =>
[nb_email_shared] =>
[period] => 1
)
[2] => Array
(
[email] => sqd@test.com
[gender] => m
[lastname] => qsd
[firstname] => qsd
[birthdate] => 1981-01-17
[country] => AT
[xgfx3r] =>
[language] => fr_FR
[newsletter] => 0
[newsletter_partner] => 0
[facebook_login] =>
[date] => 2011-08-11 15:43:22
[vol6t3] => Un militant de gauche
[uwwpma] =>
[bhqgzn] =>
[nb_email_shared] =>
[period] => 1
)
[3] => Array
(
[email] => qsd@laposte.net
[gender] => m
[lastname] => qsd
[firstname] => qsdqsd
[birthdate] => 1982-02-19
[country] => BS
[xgfx3r] =>
[language] => fr_FR
[newsletter] => 0
[newsletter_partner] => 0
[facebook_login] =>
[date] => 2011-08-11 15:45:13
[vol6t3] => Un militant de gauche
[uwwpma] =>
[bhqgzn] =>
[nb_email_shared] =>
[period] => 1
)
[4] => Array
(
[email] => qsdqs@laposte.net
[gender] => m
[lastname] => qds
[firstname] => dqs
[birthdate] => 1980-11-19
[country] => BS
[xgfx3r] =>
[language] => fr_FR
[newsletter] => 0
[newsletter_partner] => 0
[facebook_login] =>
[date] => 2011-08-11 15:46:14
[vol6t3] => Un militant de gauche
[uwwpma] => Toi
[bhqgzn] =>
[nb_email_shared] =>
[period] => 2
)
[5] => Array
(
[email] => lol@laposte.net
[gender] => m
[lastname] => Canvas
[firstname] => Peter
[birthdate] => 1987-09-13
[country] => DE
[xgfx3r] =>
[language] => fr_FR
[newsletter] => 0
[newsletter_partner] => 0
[facebook_login] => yes
[date] => 2011-08-11 15:47:11
[vol6t3] => L'homme de bernadette
[uwwpma] =>
[bhqgzn] =>
[nb_email_shared] =>
[period] => 1
)
)
非常感谢!
彼得,答案 0 :(得分:1)
如果我正确理解您的示例,则在您收到一封包含电子邮件qsdqs@laposte.net
的条目后,您需要使用相同的电子邮件弃掉以下任何内容。
应该这样做:
$result = array();
foreach ($arr as $entry) {
if (!isset($result[$entry['email']])) {
$result[$entry['email']] = $entry;
}
}
$result = array_values($result);
答案 1 :(得分:0)
foreach( $first_array as $key => &$value) {
if ($value['email'] == $second_array[$key]['email']) {
array_merge($value, $second_array[$key]);
}
}
答案 2 :(得分:0)
$merged = []
foreach($arr as $v){
$e = $v['email'];
if(array_key_exists($merged[$e]){
$merged[$e] = array_merge($v,$merged[$i]);
}else{
$merged[$e] = $v;
}
}
// Merged will now be an associative array with the e-mail addresses as keys
// if you want to use it as a normal array use
$merged = array_values($merged);
答案 3 :(得分:0)
很容易找到具有相同电子邮件地址的成员数组并“合并”它们,但挑战在于决定如何覆盖其他字段。
如果你使用array_merge()
,后面的值总是优先,但这可能不是你想要的 - 在你提供的例子中,结果数组有一些来自第一个数组的值和一些来自第二个数组( 'vol6t3'键具有第一个数组的值,但'lastname'和'firstname'键取自第二个数组。)
在我的脑海中,您将不得不循环访问数据以收集具有相同电子邮件地址的所有文章的列表,然后循环浏览该列表并根据每个字段的决定合并数据我真的无法帮助你。
其中一些很容易 - 如果字段中的数据在一个数组中而另一个数据中没有数据(如'vol6t3'键),那么您将使用数据。但是如果该字段在两个数组中都有数据,则必须决定保留哪些数据以及丢弃哪些数据(或者您可以保留两者并将该字段转换为数组)。
如果你愿意,我会提供一个代码示例,但是我需要一些时间来编写它......
答案 4 :(得分:0)
非常感谢Stanislav Shabalin真的帮助我。
我写的功能成功运行:) bellow:
/** * Get a distinct multi dimensional array * * @param array $array * array to distinct * @param string $keySearch * string key to search double array * @param boolean $overwrite = false * boolean to allow values to be overwritten * @param array $exception = array() * array of keys to not overwrite if overwrite is true * * @return array $result * Return a multi dimensional array witch has been distinct. */ public static function distinctMultiDimensionalArray($array, $keySearch, $overwrite = false, $exception = array()) { // Check if it's an array if( !is_array($array) ) return false; $result = array(); foreach ( $array as $entry ) { // If email doesn't exist if ( !isset($result[$entry[$keySearch]]) ) $result[$entry[$keySearch]] = $entry; else { // If email exist foreach ( $entry as $key => $value ) { if( !empty($value) ) { // If not empty value and this value is different from before and you don't want to overwrite values // Or you want to overwrite values except some keys if( ( !empty( $result[$entry[$keySearch]][$key] ) && $result[$entry[$keySearch]][$key] != $value && $overwrite == false ) || ( $overwrite == true && in_array($key, $exception) ) ) $result[$entry[$keySearch]][$key] = $result[$entry[$keySearch]][$key] . ', ' . $value; else $result[$entry[$keySearch]][$key] = $value; } } } } $result = array_values($result); return $result; }
非常感谢!