title: PHP Find keys in one array that exist as values in another array, and pull back the value
body: <p>I have two arrays, the first has keys, which exist as values in the second array.
  I'd like to identify the keys in the first array, that exist as values in the second array. And in the second array, i'd like to change the value (that is the key in the first array) to the value of the key in the first array.</p>
  <p><strong>First Array</strong></p>
  <pre><code>Array
  (
      **[sequence]** =&gt; 2344
      **[imbarcode]** =&gt; DTTDTADDFDTADTDTAAATDFAFTTFDDAAFFFTTAADFADDDFATADADDFATDDDTDFTDFD
      **[first]** =&gt; Lisa A
      **[last]** =&gt; Dedrick
      **[address]** =&gt; 4019 Nightington Dr
      **[city]** =&gt; Province
      **[st]** =&gt; PA
      **[zip]** =&gt; 10059-1702
  )
  </code></pre>
  <p><strong>Second Array</strong></p>
  <pre><code>Array
  (
      [deleted_on] =&gt; 
      [universe_id] =&gt; 1
      [street] =&gt; **address**
      [city] =&gt; **city**
      [state] =&gt; **st**
      [zip] =&gt; **zip**
      [check_digit] =&gt; **sequence**
      [carrier_route] =&gt; **imbarcode**
  )
  </code></pre>
  <p><strong>Desired result</strong></p>
  <pre><code>Array
  (
      [deleted_on] =&gt; 
      [universe_id] =&gt; 1
      [street] =&gt; **4019 Nightington Dr**
      [city] =&gt; **Province**
      [state] =&gt; **PA**
      [zip] =&gt; **10059-1702**
      [check_digit] =&gt; **2334**
      [carrier_route] =&gt; **DTTDTADDFDTADTDTAAATDFAFTTFDDAAFFFTTAADFADDDFATADADDFATDDDTDFTDFD**
  )
  </code></pre>
  <p>This could be done with multiple foreachs, but i'd like to find a less expensive way to do this as there could be a massively large number of arrays we're essentially mapping.</p>
  <p><strong>Full story</strong></p>
  <p>The first array, is one of what could be thousands (from a csv). So if we have 50,000 line items in the csv, we'll have 49,999 arrays. The second array, will always have four arrays (they'll correlate to a database). The end result, (with the above csv example) should have one mass array, with 4 sub arrays, where each sub-array has 49,999 arrays within it.</p>
  <p>Not very efficient with PHP however, it's what I am limited to currently.</p>
  <p>Any help is greatly appreciated.</p>
  <p>EDIT</p>
  <p>Here is what I have that atleast maps the values (does not retain the other values I need)</p>
  <pre><code>        foreach ($data as $key =&gt; $val) {
              foreach ($csv as $k =&gt; $v) {
                  foreach ($val as $dk =&gt; $dv) {
                      foreach ($v as $ck =&gt; $cv) {
                          if ($dv == $ck) {
                              $res[$dk] = $cv;
                          }
                      }
                  }
                  $result[$key][] = $res;
              }
          }
          $this-&gt;out(print_r($result,true));
  </code></pre>
  <p>I don't want to foreach this many times.</p>
  <p>SOLVED.</p>
  <p>Solution</p>
  <pre><code>foreach ($data as $key =&gt; $val) {
      foreach ($csv as $ck =&gt; $cv) {
          $result[$key][] = str_replace(array_keys($cv), $cv, $val);
      }
  }
  </code></pre>