Peterim
Peterim

Reputation: 1059

Add items from one array to another

I have two arrays:

1) The first array:

array(

    [0] => array(
              [code] => code_1
              [value] => xxx
    [1] => array(
              [code] => code_2
              [value] => xxx     
    [2] => array(
              [code] => code_3
              [value] => xxx     

2) The second array:

array(

    [0] => array(
              [settingcode] => code_1
              [value] => xxx
    [1] => array(
              [settingcode] => code_2
              [value] => xxx     
    [2] => array(
              [settingcode] => code_3
              [value] => xxx
    [3] => array(
              [settingcode] => code_4
              [value] => xxx     
    [4] => array(
              [settingcode] => code_5
              [value] => xxx    

How can add two missing items (code_4 and code_5) from array2 to array1?

Thank you!

UPD.

Sorry, I need to clarify the question a bit. I need the resulting array to look like:

array(

    [0] => array(
              [code] => code_1
              [value] => xxx
    [1] => array(
              [code] => code_2
              [value] => xxx     
    [2] => array(
              [code] => code_3
              [value] => xxx
    [3] => array(
              [code] => code_4
              [value] => xxx     
    [4] => array(
              [code] => code_5
              [value] => xxx 

Upvotes: 0

Views: 3973

Answers (3)

Timo Huovinen
Timo Huovinen

Reputation: 55643

If you can use the keys of the array to identify the value, then this will work

$resulting_array = $array2 + $array1;

Upvotes: 0

zaczap
zaczap

Reputation: 1396

function add($from, $to)
{
 foreach($from as $key => $value)
 {
  if($to[$key] == "")
       $to[$key] = $value;
 }
 return $to;
}

array_merge would be the better way, however

Upvotes: 1

jmucchiello
jmucchiello

Reputation: 18984

array_merge

Upvotes: 7

Related Questions