我有两个包含主要城市和次城市的用户不同统计信息的数组。
两个数组之间的区别是其中一个具有ofd属性,而另一个不具有ofd。另一个具有相同用户其他统计信息的数组(如已交付)。
所以我要在这里实现的是根据不同的主要城市和次城市将用户统计信息组合成一个数组...
数组1:
[310] => Array
(
[walker_id] => 158
[walker] => سلطان 158
[districts] => Al Muhammadiyah, Al Hamadaniyah, Abhur Aljanubiah, Al Basatin, Al Asalah, Ar Rayyan, Abur Ash Shamaliyah,
Al Murjan, As Salhiyah, Al Khalidiyah, Ar Rawadah, Az Zahra, Al Faisaliyah, An Nahdah, Ash Shati, Al Wahah, Al
Khaldiyah, Al Loaloa, Ar Rabwah, An Nuzhah, An Naim, King Abdul Aziz International Airport, Ash Sheraa, Az Zomorod, Al
Yaqoot, Al Amwaj, Al Hamra, Al Andalus, Al Aziziyah, As Samir, Al Fadeylah, Al Waha, An Nakhil, Mraykh, Ash Sharafiyah,
Al Marwah, Abhur Al Junoobiyah, Petoromin, An Nasim, Abrug Ar Rughamah, Al Amir Abdulmajed, Jeddah Islamic Seaport, Al
Frosya, Taibah, As Salamah, As Sarawat, Al Farouk, Ar Rewis, As Safa, Ar Rawabi, Al Baghdadiyah Al Gharbiyah, Faysalia,
Al Salamah, Mishrifah, Thuwal, Al Masarah
[main_city] => Jeddah
[sub_city] => Jeddah
[ofd] => 25
)
[311] => Array
(
[walker_id] => 158
[walker] => سلطان 158
[districts] => Al Muhammadiyah, Al Hamadaniyah, Abhur Aljanubiah, Al Basatin, Al Asalah, Ar Rayyan, Abur Ash Shamaliyah,
Al Murjan, As Salhiyah, Al Khalidiyah, Ar Rawadah, Az Zahra, Al Faisaliyah, An Nahdah, Ash Shati, Al Wahah, Al
Khaldiyah, Al Loaloa, Ar Rabwah, An Nuzhah, An Naim, King Abdul Aziz International Airport, Ash Sheraa, Az Zomorod, Al
Yaqoot, Al Amwaj, Al Hamra, Al Andalus, Al Aziziyah, As Samir, Al Fadeylah, Al Waha, An Nakhil, Mraykh, Ash Sharafiyah,
Al Marwah, Abhur Al Junoobiyah, Petoromin, An Nasim, Abrug Ar Rughamah, Al Amir Abdulmajed, Jeddah Islamic Seaport, Al
Frosya, Taibah, As Salamah, As Sarawat, Al Farouk, Ar Rewis, As Safa, Ar Rawabi, Al Baghdadiyah Al Gharbiyah, Faysalia,
Al Salamah, Mishrifah, Thuwal, Al Masarah
[main_city] => Jeddah
[sub_city] => Khulais
[ofd] => 1
)
数组2:
[260] => Array
(
[walker_id] => 158
[walker] => سلطان 158
[main_city] => Jeddah
[sub_city] => Jeddah
[districts] => Al Muhammadiyah, Al Hamadaniyah, Abhur Aljanubiah, Al Basatin, Al Asalah, Ar Rayyan, Abur Ash Shamaliyah,
Al Murjan, As Salhiyah, Al Khalidiyah, Ar Rawadah, Az Zahra, Al Faisaliyah, An Nahdah, Ash Shati, Al Wahah, Al
Khaldiyah, Al Loaloa, Ar Rabwah, An Nuzhah, An Naim, King Abdul Aziz International Airport, Ash Sheraa, Az Zomorod, Al
Yaqoot, Al Amwaj, Al Hamra, Al Andalus, Al Aziziyah, As Samir, Al Fadeylah, Al Waha, An Nakhil, Mraykh, Ash Sharafiyah,
Al Marwah, Abhur Al Junoobiyah, Petoromin, An Nasim, Abrug Ar Rughamah, Al Amir Abdulmajed, Jeddah Islamic Seaport, Al
Frosya, Taibah, As Salamah, As Sarawat, Al Farouk, Ar Rewis, As Safa, Ar Rawabi, Al Baghdadiyah Al Gharbiyah, Faysalia,
Al Salamah, Mishrifah, Thuwal, Al Masarah
[delivered] => 16
[undelivered] => 3
[returnedToowner] => 0
)
[261] => Array
(
[walker_id] => 158
[walker] => سلطان 158
[main_city] => Jeddah
[sub_city] => Khulais
[districts] => Al Muhammadiyah, Al Hamadaniyah, Abhur Aljanubiah, Al Basatin, Al Asalah, Ar Rayyan, Abur Ash Shamaliyah,
Al Murjan, As Salhiyah, Al Khalidiyah, Ar Rawadah, Az Zahra, Al Faisaliyah, An Nahdah, Ash Shati, Al Wahah, Al
Khaldiyah, Al Loaloa, Ar Rabwah, An Nuzhah, An Naim, King Abdul Aziz International Airport, Ash Sheraa, Az Zomorod, Al
Yaqoot, Al Amwaj, Al Hamra, Al Andalus, Al Aziziyah, As Samir, Al Fadeylah, Al Waha, An Nakhil, Mraykh, Ash Sharafiyah,
Al Marwah, Abhur Al Junoobiyah, Petoromin, An Nasim, Abrug Ar Rughamah, Al Amir Abdulmajed, Jeddah Islamic Seaport, Al
Frosya, Taibah, As Salamah, As Sarawat, Al Farouk, Ar Rewis, As Safa, Ar Rawabi, Al Baghdadiyah Al Gharbiyah, Faysalia,
Al Salamah, Mishrifah, Thuwal, Al Masarah
[delivered] => 0
[undelivered] => 1
[returnedToowner] => 0
所需结果:
[0] => Array
(
[walker_id] => 158
[walker] => سلطان 158
[main_city] => Jeddah
[sub_city] => Jeddah
[districts] => Al Muhammadiyah, Al Hamadaniyah, Abhur Aljanubiah, Al Basatin, Al Asalah, Ar Rayyan, Abur Ash Shamaliyah,
Al Murjan, As Salhiyah, Al Khalidiyah, Ar Rawadah, Az Zahra, Al Faisaliyah, An Nahdah, Ash Shati, Al Wahah, Al
Khaldiyah, Al Loaloa, Ar Rabwah, An Nuzhah, An Naim, King Abdul Aziz International Airport, Ash Sheraa, Az Zomorod, Al
Yaqoot, Al Amwaj, Al Hamra, Al Andalus, Al Aziziyah, As Samir, Al Fadeylah, Al Waha, An Nakhil, Mraykh, Ash Sharafiyah,
Al Marwah, Abhur Al Junoobiyah, Petoromin, An Nasim, Abrug Ar Rughamah, Al Amir Abdulmajed, Jeddah Islamic Seaport, Al
Frosya, Taibah, As Salamah, As Sarawat, Al Farouk, Ar Rewis, As Safa, Ar Rawabi, Al Baghdadiyah Al Gharbiyah, Faysalia,
Al Salamah, Mishrifah, Thuwal, Al Masarah
[delivered] => 16
[undelivered] => 3
[returnedToowner] => 0
[ofd] => 53
)
[1] => Array
(
[walker_id] => 158
[walker] => سلطان 158
[main_city] => Jeddah
[sub_city] => Khulais
[districts] => Al Muhammadiyah, Al Hamadaniyah, Abhur Aljanubiah, Al Basatin, Al Asalah, Ar Rayyan, Abur Ash Shamaliyah,
Al Murjan, As Salhiyah, Al Khalidiyah, Ar Rawadah, Az Zahra, Al Faisaliyah, An Nahdah, Ash Shati, Al Wahah, Al
Khaldiyah, Al Loaloa, Ar Rabwah, An Nuzhah, An Naim, King Abdul Aziz International Airport, Ash Sheraa, Az Zomorod, Al
Yaqoot, Al Amwaj, Al Hamra, Al Andalus, Al Aziziyah, As Samir, Al Fadeylah, Al Waha, An Nakhil, Mraykh, Ash Sharafiyah,
Al Marwah, Abhur Al Junoobiyah, Petoromin, An Nasim, Abrug Ar Rughamah, Al Amir Abdulmajed, Jeddah Islamic Seaport, Al
Frosya, Taibah, As Salamah, As Sarawat, Al Farouk, Ar Rewis, As Safa, Ar Rawabi, Al Baghdadiyah Al Gharbiyah, Faysalia,
Al Salamah, Mishrifah, Thuwal, Al Masarah
[delivered] => 0
[undelivered] => 1
[returnedToowner] => 0
[ofd] => 1
编辑:
我的代码:
foreach($delivered as $value){
$def[] = array(
'walker_id' => $value->confirmed_walker,
'walker' => $value->walker,
'main_city' => $value->main_city,
'sub_city' => $value->d_city,
'districts' => $value->districts,
'delivered' => $value->delivered,
'undelivered' => $value->undelivered,
'returnedToowner' => $value->toowner,
);
}
foreach($withCaptain as $value){
$dataArray[] = array(
'walker_id' => $value->walker_id,
'walker' => $value->walker,
'districts' => $value->districts,
'main_city' => $value->main_city,
'sub_city' => $value->sub_city,
'ofd' => $value->ofd,
);
}
$kv=[];
foreach($dataArray as $k => $v){
$kv[ $v['walker_id'] ] = $k; // this is considering unique walker_id and not considering city and subcity
}
foreach($def as $k => $v){
if(!isset($dataArray[$k]['delivered'])){
$dataArray[$k]['delivered'] = 0;
$dataArray[$k]['undelivered'] = 0;
}
if( array_key_exists( $v['walker_id'] , $kv ) ){
$dataArray[ $kv[$v['walker_id']] ] = array_merge( $dataArray[$kv[$v['walker_id']]] , $def[$k] );
}
}
想要合并$ def和$ dataArray。它们都是非相等的数组。
所以基本上我想结合基于walker_id,main_city和sub_city的两个数组。
我尝试了各种各样的事情,但是没有任何结果。我已经试过了 Merge two associative arrays by same key
但是临时数组不考虑主要城市和次要城市,其只是基于walker_id ...
预先感谢
如果您需要更多详细信息,请告诉我。.
答案 0 :(得分:1)
这将是一种简单的方法:
<?php
$withOfd = [
310 => [
'walker_id' => 158,
'walker' => "158 ??????????",
'districts' => "...",
'main_city' => "Jeddah",
'sub_city' => "Jeddah",
'ofd' => 25
],
311 => [
'walker_id' => 158,
'walker' => "158 ??????????",
'districts' => "...",
'main_city' => "Jeddah",
'sub_city' => "Khulais",
'ofd' => 1
]
];
$withoutOfd = [
260 => [
'walker_id' => 158,
'walker' => "158 ??????????",
'main_city' => "Jeddah",
'sub_city' => "Jeddah",
'districts' => "...",
'delivered' => 16,
'undelivered' => 3,
'returnedToowner' => 0
],
261 => [
'walker_id' => 158,
'walker' => "158 ??????????",
'main_city' => "Jeddah",
'sub_city' => "Khulais",
'districts' => "...",
'delivered' => 0,
'undelivered' => 1,
'returnedToowner' => 0
]
];
$result = [];
array_walk($withOfd, function($with) use ($withoutOfd, &$result) {
array_walk ($withoutOfd, function($without) use (&$result, $with) {
if ( $with['walker_id'] == $without['walker_id']
&& $with['main_city'] == $without['main_city']
&& $with['sub_city'] == $without['sub_city']) {
$result[] = array_merge($with, $without);
}
});
});
print_r($result);
输出显然是:
Array
(
[0] => Array
(
[walker_id] => 158
[walker] => 158 ??????????
[districts] => ...
[main_city] => Jeddah
[sub_city] => Jeddah
[ofd] => 25
[delivered] => 16
[undelivered] => 3
[returnedToowner] => 0
)
[1] => Array
(
[walker_id] => 158
[walker] => 158 ??????????
[districts] => ...
[main_city] => Jeddah
[sub_city] => Khulais
[ofd] => 1
[delivered] => 0
[undelivered] => 1
[returnedToowner] => 0
)
)
更新:
由于评论和聊天中的讨论,另一个变体将不是具有相应条目的条目保留在两个数组中:
$result = [];
array_walk($withOfd, function($with, $withKey) use (&$withOfd, &$withoutOfd, &$result) {
array_walk ($withoutOfd, function($without, $withoutKey) use (&$withOfd, &$withoutOfd, &$result, $with, $withKey) {
if ( $with['walker_id'] == $without['walker_id']
&& $with['main_city'] == $without['main_city']
&& $with['sub_city'] == $without['sub_city']) {
$result[] = array_merge($with, $without);
unset($withOfd[$withKey]);
unset($withoutOfd[$withoutKey]);
}
});
});
$result = array_merge($result, $withOfd, $withoutOfd);