title: create an array based on two table id's in php
body: <p>I have two tables. one is 'demo' and other is 'like'.</p>
  
  <p>I am converting array of values into json format using json_encode.</p>
  
  <p>Demo has : </p>
  
  <pre><code>[{ 'id': 1, 'like_id': 2, 'name': 'hero' }, { 'id': 2, 'like_id': 1, 'name': 'villain' }]
  </code></pre>
  
  <p>Like has :</p>
  
  <pre><code>[{ 'id': 1, 'movie'': 'castle' }, {'id': 2, 'movie' : 'superman' }]
  </code></pre>
  
  <p>I want to create a new json data as follows:</p>
  
  <pre><code>[{ 'id': 1, 'like_id': [{'id': 2, 'movie' : 'superman'}], 'name': 'hero'},
   { 'id': 2, 'like_id': [{'id': 1, 'movie': 'castle'}], 'name': 'villain'}]
  </code></pre>
  
  <p>After searching for a while, I think i might need recursive function. But I am not sure how i can write in this scenario.</p>
  
  <p>This is what i tried : </p>
  
  <pre><code>$pages = array();
  
  $demo = Object('demo');
  $like = Object('like');
  
  foreach ($demo as $d) {
      foreach ($like as $l) {
          $response = array(
             'id' =&gt; $d['id'],
             'like_id' =&gt; array(
                   'id' =&gt; $l['id'],
                   'movie' =&gt; $l['movie']
             ),
             'name' =&gt; $d['name']
          );
          array_push($pages, $response);
       }
    }
  $res = json_encode($pages);
  echo $res;
  </code></pre>