2017-06-02 72 views
1

這裏是我的兩個數組 用戶陣列合併兩個數組並返回一個結果

$users = [ 
[ 
    'name' => 'Bikash', 
    'city_id' => 1 
], 
[ 
    'name' => 'Riaz', 
    'city_id' => 3 
], 
[ 
    'name' => 'Sayantan', 
    'city_id' => 2 
], 
[ 
    'name' => 'Subha', 
    'city_id' => 1 
], 
[ 
    'name' => 'Amit', 
    'city_id' => 2 
] 
]; 

城市陣列

$cities = [ 
[ 
    'id' => 1, 
    'name' => 'Kolkata' 
], 
[ 
    'id' => 2, 
    'name' => 'Bangalore' 
], 
[ 
    'id' => 3, 
    'name' => 'Mumbai' 
], 
]; 

我想city_id與用戶陣列城市名來代替。

樣本輸出

$users = [ 
[ 
'name' => 'Bikash', 
'city' => 'Kolkata' 
], 
[ 
'name' => 'Riaz', 
'city' => 'Mumbai' 
], 
[ 
'name' => 'Sayantan', 
'city_id' => 'Bangalore' 
], 
[ 
'name' => 'Subha', 
'city' => 'Kolkata' 
], 
[ 
'name' => 'Amit', 
'city' => 'Bangalore' 
] 
]; 

這是我迄今

$userNew = []; 

foreach ($users as $user): 
    $userNew[$user['name']] = $cities[0]['name']; 
endforeach; 

echo '<pre>'; 
print_r($userNew); 

我找不到這個特定問題的任何解決方案的嘗試。

回答

2

將溶液使用array_columnarray_walk功能:

$city_names = array_column($cities, 'name', 'id'); 
array_walk($users, function(&$v, $k) use($city_names){ 
    if (isset($city_names[$v['city_id']])) { 
     $v['city_id'] = $city_names[$v['city_id']]; 
    } 
}); 

print_r($users); 

輸出:

Array 
(
    [0] => Array 
     (
      [name] => Bikash 
      [city_id] => Kolkata 
     ) 

    [1] => Array 
     (
      [name] => Riaz 
      [city_id] => Mumbai 
     ) 

    [2] => Array 
     (
      [name] => Sayantan 
      [city_id] => Bangalore 
     ) 

    [3] => Array 
     (
      [name] => Subha 
      [city_id] => Kolkata 
     ) 

    [4] => Array 
     (
      [name] => Amit 
      [city_id] => Bangalore 
     ) 
) 
3

您可以製作一個城市ID和城市名稱對的數組。 Live demo here

$map = array_combine(array_column($cities, 'id'), array_column($cities, 'name')); 
foreach($users as &$v) 
{ 
    $v['city_id'] = $map[$v['city_id']]; 
} 

更清晰的方式來計算$mapanswer of RomanPerekhrest

$map = array_column($cities, 'name', 'id');

0
<?php 
    $newCities=array(); 
    //resort cities, so we can get the values directly 
    foreach($cities as $key => $value) 
    { 
     $newCities[$key]=$value; 
    } 
    $newUser=array(); 
    //now we can go over user 
    foreach($user as $key => $value) 
    { 
    $newData['name']=$value['name']; 
    $newData['city']=$newCities[$value['city_id']]; 
    $newUser[$key]=$newData; 
    } 
0

你可以使用嵌套循環來做到這一點,但它會是一個漫長的過程。

一個更好的辦法來做到這一點是:

$newCitties = []; 
foreach($cities as $city){ 
    $newCitties[$city['id']] = $city['name']; 
} 

foreach($users as $user){ 
    $user['city'] = $newCitties[$user['city_id']]; 
    unset($user['city_id']); 
} 
0

請試試這個

$newuser = ""; 
function getCityname($cities,$cityid){ 

    foreach($cities as $city){ 
     if($city['id'] == $cityid) return $city['name']; 
    } 
} 

foreach($users as $user){ 
    $cityname = getCityname($cities,$user['city_id']); 
    $newuser[] = array('name' => $user['name'] , 'city' => $cityname); 
} 

print_r($newuser); 
0
$_cities = array_combine(array_column($cities, 'id'), array_column($cities, 'name')); 

$userNew = []; 

foreach ($users as $user): 
    $userNew[] = ['name' => $user['name'], 'city' => $_cities[$user['city_id']]]; 
endforeach; 
+1

感謝您對這個代碼片段,它可以提供即時幫助。通過展示*爲什麼*這是一個很好的解決方案,對未來的讀者會有更好的解決方案,這將爲它的教育價值提供一個合適的解釋[//大大提高](// meta.stackexchange.com/q/114762)但不完全相同的問題。請編輯您的答案以添加解釋,並指出適用的限制和假設。 –