title: How to merge two multi array without duplicates in PHP
body: <p>I'm trying to merge two Multiple Arrays.</p>
  <p>Both arrays have the same item(<code>{ &quot;ort&quot;: &quot;rom&quot;, &quot;lioc&quot;: &quot;inrom&quot; }</code>).</p>
  <pre><code>    $test_1 =  '
      {
          &quot;hotels&quot; : { 
              &quot;hotel&quot; : [
                  { &quot;ort&quot;: &quot;rom&quot;, &quot;lioc&quot;: &quot;inrom&quot; },
                  { &quot;ort&quot;: &quot;paris&quot;, &quot;lioc&quot;: &quot;inpsrisd&quot; },
                  { &quot;ort&quot;: &quot;berlin&quot;, &quot;lioc&quot;: &quot;inberlin&quot; },
                  { &quot;ort&quot;: &quot;milano&quot;, &quot;lioc&quot;: &quot;inmilano&quot; },
                  { &quot;ort&quot;: &quot;paris&quot;, &quot;lioc&quot;: &quot;anotherinpsrisd&quot; },
                  { &quot;ort&quot;: &quot;muc&quot;, &quot;lioc&quot;: &quot;inmuc&quot; }
              ]
          }
      }
      ';
      $test_2 =  '
      {
          &quot;hotels&quot; : { 
              &quot;hotel&quot; : [
                  { &quot;ort&quot;: &quot;rom&quot;, &quot;lioc&quot;: &quot;inrom&quot; },
                  { &quot;ort&quot;: &quot;beijing&quot;, &quot;lioc&quot;: &quot;wanda&quot; }
              ]
          }
      }
      ';
      
      $arr_test1 = json_decode($test_1, TRUE);
      $arr_test2 = json_decode($test_2, TRUE);
  
      var_dump(json_encode(array_merge_recursive($arr_test1, $arr_test2)));
      var_dump(json_encode(array_unique(array_merge($arr_test1,$arr_test2), SORT_REGULAR)));
  </code></pre>
  <p>If I run this code then I get the following results.</p>
  <pre><code>result1:
  {
      &quot;hotels&quot; : { 
          &quot;hotel&quot; : [
              { &quot;ort&quot;: &quot;rom&quot;, &quot;lioc&quot;: &quot;inrom&quot; },
              { &quot;ort&quot;: &quot;paris&quot;, &quot;lioc&quot;: &quot;inpsrisd&quot; },
              { &quot;ort&quot;: &quot;berlin&quot;, &quot;lioc&quot;: &quot;inberlin&quot; },
              { &quot;ort&quot;: &quot;milano&quot;, &quot;lioc&quot;: &quot;inmilano&quot; },
              { &quot;ort&quot;: &quot;paris&quot;, &quot;lioc&quot;: &quot;anotherinpsrisd&quot; },
              { &quot;ort&quot;: &quot;muc&quot;, &quot;lioc&quot;: &quot;inmuc&quot; },
              {&quot;ort&quot;:&quot;rom&quot;,&quot;lioc&quot;:&quot;inrom&quot;},
              {&quot;ort&quot;:&quot;beijing&quot;,&quot;lioc&quot;:&quot;wanda&quot;}
          ]
      }
  }
  
  result2:
  {
      &quot;hotels&quot; : { 
          &quot;hotel&quot; : [
              { &quot;ort&quot;: &quot;rom&quot;, &quot;lioc&quot;: &quot;inrom&quot; },
              { &quot;ort&quot;: &quot;beijing&quot;, &quot;lioc&quot;: &quot;wanda&quot; }
          ]
      }
  }
  </code></pre>
  <p>Both are not my expected results.</p>
  <p><a href="https://i.stack.imgur.com/0bCLM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0bCLM.png" alt="enter image description here" /></a></p>
  <p>As you can see in this screenshot <code>{ &quot;ort&quot;: &quot;rom&quot;, &quot;lioc&quot;: &quot;inrom&quot; }</code> item is duplicated in <em>result1</em>.</p>
  <p>The point is that don't know what is array's keys are and don't know deep of the array.</p>
  <p>I'd like to get the merged array without duplicated items like this.</p>
  <pre><code>{
      &quot;hotels&quot; : { 
          &quot;hotel&quot; : [
              { &quot;ort&quot;: &quot;rom&quot;, &quot;lioc&quot;: &quot;inrom&quot; },
              { &quot;ort&quot;: &quot;paris&quot;, &quot;lioc&quot;: &quot;inpsrisd&quot; },
              { &quot;ort&quot;: &quot;berlin&quot;, &quot;lioc&quot;: &quot;inberlin&quot; },
              { &quot;ort&quot;: &quot;milano&quot;, &quot;lioc&quot;: &quot;inmilano&quot; },
              { &quot;ort&quot;: &quot;paris&quot;, &quot;lioc&quot;: &quot;anotherinpsrisd&quot; },
              { &quot;ort&quot;: &quot;muc&quot;, &quot;lioc&quot;: &quot;inmuc&quot; },
              { &quot;ort&quot;: &quot;beijing&quot;, &quot;lioc&quot;: &quot;wanda&quot; }
          ]
      }
  }
  </code></pre>
  <p>How can I solve this?</p>