tim peterson
tim peterson

Reputation: 24305

How in PHP to add values from one array to another when their key value pairs match?

Here are my two arrays:

$array1 =( [0] => Array ( [row_id] => 237 [comment] => 'hello0' )
           [1] => Array ( [row_id] => 5 [comment] => 'hello1' ) 
           [2] => Array ( [row_id] => 6 [comment] => 'hello2' ) );

$array2= ( [0] => Array ( [row_id] => 5 [vote] => 1 ) 
           [1] => Array ( [row_id] => 7 [vote] => 1 ) 
           [2] => Array ( [row_id] => 237 [vote] => 0 ) );

I'd like to match $array1 and $array2 on [row_id] and add $array2's [vote] key/value pairs to $array1 where $array1[row_id]=$array2[row_id]

This is how I'd like the output to be:

$array1 =( [0] => Array ( [row_id] => 237 [comment] => 'hello0' [vote] => 0  )
           [1] => Array ( [row_id] => 5 [comment] => 'hello1' [vote] => 1 ) 
           [2] => Array ( [row_id] => 6 [comment] => 'hello2' [vote] => 1 ) );

I'm sure there are many ways to do this, so thoughts on the fastest computationally would also be appreciated!

Upvotes: 1

Views: 5429

Answers (2)

Malovich
Malovich

Reputation: 971

foreach($array1 as $key1=>$value1)
{
    foreach($array2 as $key2=>$value2)
   {
       if($value1['row_id']==$value2['row_id'])
       {
           if ($value2['vote']) {
               $result[$key1]['vote']=$value2['vote']; // You're assigning the vote value to a new index of 'vote' on the original array.
           } else {
               $result[$key1]['vote'] = 'no vote';
           }
       }
   }
}

This is the change that is needed in Ray Cheng's answer.

EDIT'd

EDIT'd again:

When pulling data from a database, you can definitely get the records as an array (look it up, its as old as the hills, the code is out there). The next step is reorganizing the array into the preferred format. FOREACH is ideal for this.

// $array1 brought in from some other process
$arrayStorage = array();
foreach ($array1 as $row){ 
    $arrayStorage[$row['row_id']] = array('votes'=>$row['votes'], 'comment'=>$row['comment']);
}

When you want to put it back into the database, reverse it, making sure you pull the keys out again.

foreach ($arrayStorage as $row_id=>$row_data){ ...

EDIT the LAST:

Assuming that both respective databases have been pulled for data into the OP's format...

foreach ($array1 as $row){ 
    $arrayStorage[$row['row_id']]['comment'] = $row['comment'];
}

foreach ($array2 as $row){ 
    $arrayStorage[$row['row_id']]['votes'] = $row['votes'];
}
$array1 = $arrayStorage;

// You are going through $array1 and $array2 and creating a placeholder that is built with the $row_id as an associated structure with a comment and vote for each $row_id. This is your final desired array.

Upvotes: 2

Raymond Cheng
Raymond Cheng

Reputation: 2505

foreach($array1 as $key1=>$value1)
{
  foreach($array2 as $key2=>$value2)
  {
     if($value1['row_id']==$value2['row_id'])
     {
         $value1['vote'] = $value2['vote'];
         $result[$key1][]=$value1;
     }
 }
}

$result is what you need!

Upvotes: 6

Related Questions