Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>foreach($array1 as $key1=&gt;$value1) { foreach($array2 as $key2=&gt;$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'; } } } } </code></pre> <p>This is the change that is needed in Ray Cheng's answer.</p> <p>EDIT'd</p> <p>EDIT'd again:</p> <p>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. <code>FOREACH</code> is ideal for this.</p> <pre><code>// $array1 brought in from some other process $arrayStorage = array(); foreach ($array1 as $row){ $arrayStorage[$row['row_id']] = array('votes'=&gt;$row['votes'], 'comment'=&gt;$row['comment']); } </code></pre> <p>When you want to put it back into the database, reverse it, making sure you pull the keys out again.</p> <pre><code>foreach ($arrayStorage as $row_id=&gt;$row_data){ ... </code></pre> <p>EDIT the LAST:</p> <p>Assuming that both respective databases have been pulled for data into the OP's format...</p> <pre><code>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. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload