Home > Software design >  How to merge two array if one item exist in both array?
How to merge two array if one item exist in both array?

Time:11-05

I want to expand my city array with post code value. If the city_postcode array contain city array name record then push postcode value into city array. That's what i want to achive somehow.

city array:

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

city_postcode array:

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

The result I want:

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

CodePudding user response:

If you can rely on the cities and post codes to be equal in length and sorted, you could just do something like this:

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

Other wise you could do something more dyanmic like:

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

CodePudding user response:

As an alternative you can use 'reference' PHP in foreach loop as follows

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

CodePudding user response:

You can use this simple approach :

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

Note that the $city variable has been passes by reference

  • Related