2

I have one array with state codes and their full names, like this:

$first_array=array("AB"=>"Alberta","BC"=>"British Columbia");

I have another array with ids and state codes, like this:

$result_array=array(15=>array("ad_id"=>15,"state code"=>"AB"));

I want to replace the state code values in $result_array with the corresponding "full name" in $first_array.

If there is no corresponding value in $first_array then the state code in $result_array should remain unchanged.

This is my expected result:

$result_array=array(15=>array("ad_id"=>15,"state code"=>"Alberta"));
5
  • Below is my result array Commented Jun 20, 2017 at 11:36
  • my result array, Array ( [15] => array ( [ad_id] => 15 [state code] => AB ) ) but I want below array Array ( [15] => array ( [ad_id] => 15 [state code] => Alberta ) ) Commented Jun 20, 2017 at 11:37
  • try using array_search() for finding key value and replace it with another variable Commented Jun 20, 2017 at 11:39
  • 1
    array_walk($resultArray, function (&$value) use ($first_array) { $value['state_code'] = $first_array[$value['state_code']]; }); Commented Jun 20, 2017 at 11:45
  • array_walk is better. Missed it. :) Commented Jun 20, 2017 at 11:50

2 Answers 2

1

This should work -

$first_array= array("AB"=>"Alberta","BC"=>"British Columbia");

$second_array = array(
15 => array ( 'ad_id' => 15, 'state code' => 'AB' ) ,
16 => array ( 'ad_id' => 16, 'state code' => 'CD' ) 
);

$new = array_map(function($a) use($first_array) {
    return array(
        'ad_id' => $a['ad_id'],
        'state code' => !empty($first_array[$a['state code']]) ? $first_array[$a['state code']] : $a['state code'],
    );
}, $second_array);

print_r($new);

Output

Array
(
    [15] => Array
        (
            [ad_id] => 15
            [state code] => Alberta
        )

    [16] => Array
        (
            [ad_id] => 16
            [state code] => CD
        )

)
Sign up to request clarification or add additional context in comments.

Comments

0

Input:

$first_array=["AB"=>"Alberta","BC"=>"British Columbia"];
$result_array=[
    15=>['ad_id'=>15,'state code'=>'AB'],
    16=>['ad_id'=>16,'state code'=>'BC'],
    17=>['ad_id'=>17,'state code'=>'NY']
];

Method #1 - array_walk()

array_walk($result_array,function(&$a)use($first_array){
    if(isset($first_array[$a['state code']])){  // only overwrite state code if full name is available
        $a['state code']=$first_array[$a['state code']];
    }
});

Method #2 - foreach()

foreach($result_array as $k=>$a){
    if(isset($first_array[$a['state code']])){  // only overwrite state code if full name is available
        $result_array[$k]['state code']=$first_array[$a['state code']];
    }   
}

Output (from either method): var_export($result_array);

array (
  15 => 
  array (
    'ad_id' => 15,
    'state code' => 'Alberta',
  ),
  16 => 
  array (
    'ad_id' => 16,
    'state code' => 'British Columbia',
  ),
  17 => 
  array (
    'ad_id' => 17,
    'state code' => 'NY',
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.