title: Use Variables inside Array Map Function PHP
body: <p>My task is to get two create two arrays:</p>
  
  <p>Array_1 = list of all the possible options for option one</p>
  
  <p>Array_2 = list of all the possible options for option two when option one == Array_1[0]</p>
  
  <p>Given these rows...</p>
  
  <pre><code>    $rows = [
          (object) ['option_one' =&gt; 'large mug', 'option_two' =&gt; 'one color print'],
          (object) ['option_one' =&gt; 'large mug', 'option_two' =&gt; 'two color print' ],
          (object) ['option_one' =&gt; 'large mug', 'option_two' =&gt; 'three color print' ],
          (object) ['option_one' =&gt; 'small mug', 'option_two' =&gt; 'one color print' ],
          (object) ['option_one' =&gt; 'small mug', 'option_two' =&gt; 'two color print' ],
      ];
  </code></pre>
  
  <p>Then my output would be </p>
  
  <p>Array_1 = [ 'large mug', 'small mug']</p>
  
  <p>Array_2 = [ 'one color print', 'two color print', 'three color print' ]</p>
  
  <p>I have tried to accomplish this using array maps as follows...</p>
  
  <pre><code>    $option_one_arr = array_unique ( array_map(function($row) { return $row-&gt;option_one; }, $rows) );
      $option_two_arr = array_unique ( array_map(function($row) {
          // ($option_one_arr == NULL) == TRUE
          if ($row-&gt;option_one === $option_one_arr[0]) 
              return $row-&gt;option_two;
      }, $rows) );
      $to_render = [$option_one_arr, $option_two_arr];
  
      echo '&lt;pre&gt;';
      var_dump($to_render);
  </code></pre>
  
  <p>However, $option_one_arr always = NULL inside the second array map despite being correct outside the second array map.</p>
  
  <p>Thoughts?</p>