合并两个数组并返回一个结果集

时间:2017-06-02 06:42:29

标签: php arrays multidimensional-array

这是我的两个数组 用户数组

$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 Array

$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);

我找不到任何针对此特定问题的解决方案。

6 个答案:

答案 0 :(得分: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']];
}

answer of RomanPerekhrest

计算$map的更清晰的方法

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

答案 1 :(得分: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
        )
)

答案 2 :(得分: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;
  }

答案 3 :(得分: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']);
}

答案 4 :(得分: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);

答案 5 :(得分: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;