【问题标题】:How to merge two array if one item exist in both array?如果两个数组中都存在一项,如何合并两个数组?
【发布时间】:2021-12-19 00:34:16
【问题描述】:

我想用邮政编码值扩展我的城市数组。 如果 city_postcode 数组包含 city 数组名称记录,则将邮政编码值推送city 数组中。这就是我想以某种方式实现的目标。

城市数组:

Array
(
    [0] => Array
        (
            [id] => 1
            [city] => Budapest
            [population] => 1700000
        )
    [1] => Array
        (
            [id] => 2
            [city] => Szeged
            [population] => 160000
        )
)

city_postcode 数组:

Array
(
    [0] => Array
        (
            [name] => Budapest
            [post_code] => 12345
        )
    [1] => Array
        (
            [name] => Szeged
            [post_code] => 33356
        )    
)

我想要的结果:

Array
(
    [0] => Array
        (
            [id] => 1
            [city] => Budapest
            [population] => 1700000
            [post_code] => 12345
        )
    [1] => Array
        (
            [id] => 2
            [city] => Szeged
            [population] => 160000
            [post_code] => 33356
        )
)

【问题讨论】:

  • 先写一段代码,这里没人会帮你做的。

标签: php html sql arrays


【解决方案1】:

如果您可以依赖城市和邮政编码的长度和排序相同,您可以这样做:

<?php

$cities = array(
  array("id"=>1,"city"=>"Budapest","Population"=>"1700000"),
  array("id"=>2,"city"=>"Szeged","Population"=>"160000")
);
$cityPostCode = array(
  array("name"=>"Budapest","post_code"=>12345),
  array("name"=>"Szeged","post_code"=>33356)
);

for($i = 0; $i < count($cities); $i++){
  $cities[$i]['post_code'] = $cityPostCode[$i]['post_code'];
}

print_r($cities);

否则,您可以做一些更动态的事情,例如:

function _parsePostCode($targetName,$targetArr){
  foreach($targetArr as $el){
    if($el['name'] == $targetName){
      return $el['post_code'];
    }
  }
  return false;
}

for($i = 0; $i < count($cities); $i++){
  $cities[$i]['post_code'] = _parsePostCode($cities[$i]['city'],$cityPostCode);
}
print_r($cities);

【讨论】:

    【解决方案2】:

    作为替代方案,您可以在 foreach 循环中使用“参考”PHP,如下所示

    $city = array(
     0 => array(
    'id' => 1,
    'city' => "Budapest",
    'population' => 1700000
    ),
    1 => array(
    'id' => 2,
    'city' => "Szeged",
    'population' => 160000
    )
    );
    
    $city_postcode = array(
    0 =>array(
    'name' => 'Budapest',
    'post_code' => 12345
    ),
    1 => array(
    'name' => 'Szeged',
    'post_code' => 33356
    )
    );
    
    foreach ($city as $ckey => &$cval) {
     $cval['post_code'] = $city_postcode[$ckey]['post_code'];
    }
    unset($cval);
    
    var_dump($city);
    

    【讨论】:

      【解决方案3】:

      你可以使用这个简单的方法:

      <?php
      
      $city = array(
          0 => array(
              'id' => 1,
              'city' => "Budapest",
              'population' => 1700000
          ),
          1 => array(
              'id' => 2,
              'city' => "Szeged",
              'population' => 160000
          )
      );
      
      $city_postcode = array(
          0 => array(
              'name' => 'Budapest',
              'post_code' => 12345
          ),
          1 => array(
              'name' => 'Szeged',
              'post_code' => 33356
          )
      );
      
      function apply_post_code(&$item, $key, $postcode)
      {
          $item['post_code'] = $postcode[ $key ][ 'post_code' ];
      }
      
      array_walk($city, 'apply_post_code', $city_postcode);
      
      var_dump($city);
      

      注意 $city 变量是通过引用传递的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-02
        • 2019-06-13
        • 1970-01-01
        • 2020-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多