title: comparing values in a multidimensional array
body: <p>I have two arrays of arrays that have the keys 'menu_title' and 'id'.</p>
  
  <p>I want to go through the two arrays and store in a third array the id of any arrays in the first array that shares a 'menu_title' with an array in the second array.</p>
  
  <p>I know I can do it like so:</p>
  
  <pre><code>$collision = [];
  foreach($children as $child)
      foreach($siblings as $sibling)
          if($child['menu_title'] == $sibling['menu_title'])
              $collision[] = $child['id'];
  </code></pre>
  
  <p>But that means I am looping over the second array for every single item in the first.</p>
  
  <p>Perhaps this would be better?</p>
  
  <pre><code>$collision = [];
  foreach($siblings as &amp;$sibling)
          $sibling = $sibling['menu_title'];
  
      foreach($children as $child)
          if(in_array($child['menu_title'], $siblings))
              $collision[] = $child['id'];
  </code></pre>
  
  <p>But I still think there must be a better way?</p>
  
  <hr />
  
  <h2>update</h2>
  
  <p>The arrays are populated by sql, basically if I delete a category then I move the children categories up to the same level as the category I am deleting.</p>
  
  <p>But If any of the children have the same menu_title as one of the deleting categories siblings then I need to rename the menu_title of the child to "whatever-1" e.g. "computers-1"</p>
  
  <p>So I am building an array of id's I need to update and then I will do an sql update on those ID's possibly there is an enhancement to be made to how I am approaching this?</p>
  
  <hr />
  
  <h2>Update</h2>
  
  <p>So, in the end I ended up with:</p>
  
  <pre><code>$id = 4;
  $category = $this-&gt;categoryeditormodel-&gt;getCategory($id);
  $children = $this-&gt;categoryeditormodel-&gt;getChildCategories($id);
  $siblings = $this-&gt;categoryeditormodel-&gt;getSiblingCategories($id);
  
  foreach($siblings as &amp;$sibling)
      $sibling = $sibling['menu_title'];
  
  foreach($children as &amp;$child){
      $child['parent_id'] = $category['parent_id'];
      if(in_array($child['menu_title'], $siblings)){
          $i = 0;
          while(in_array(($name = ($child['menu_title'].'-'.++$i)), $siblings));
          $child['menu_title'] = $name;
      }
  }
  $this-&gt;categoryeditormodel-&gt;update_batch($children);
  </code></pre>
  
  <p>The three functions at the top do what they say, but they are cheap as the categories are already loaded into a cache so it is not another sql query.</p>
  
  <p>and update_batch is simply a shortcut to Code Igniters update_batch function but it passes through the table name and the id key.</p>
  
  <p>any thoughts?</p>