title: not success create jSon in php
body: <p>i have a question,<br>
  i created multiple json from array data,
  but i have issue output,
  json child or submenu always print every element, 
  that json child should be print in on relation in field media_menu_parent_id</p>
  
  <p>may you help me?</p>
  
  <p>i can post my image table, coz my reputaion &lt; 10 :(</p>
  
  <p>this is my code controller</p>
  
  <pre><code>header("Content-type: application/json; charset=utf-8");
      $main_arr = array();
  
      if (!$mediaId) {
          $main_arr['status'] = "nok";
          $main_arr['message'] = "Media id is null";
          $json = json_encode($main_arr);
          echo $json;
          exit();
      }
  
      $where = array( 'media_menu.status'=&gt;1 ,
                      'media_menu.media_menu_parent_id'=&gt;0,
                      'media_menu.media_id'=&gt;$mediaId
                    );
  
      $count = $this-&gt;mediamenumodel-&gt;getCountMediaMenu($where);
      $result = $this-&gt;mediamenumodel-&gt;getMediaMenu($where,$offest,$page);
  
      if (!$result) {
          $main_arr['status'] = "nok";
          $main_arr['message'] = "Data not Found";
          $json = json_encode($main_arr);
          echo $json;
          exit();
      }
  
      $main_arr['rowCount'] = $count;
      $main_arr['media_id'] = $result[0]['media_id'];
      $main_arr['media_name'] = $result[0]['media_name'];
  
      $h=0;
      foreach($result as $key =&gt; $value)
      {    
          $where1 = array('media_menu_parent_id' =&gt; $value['media_menu_id']);
          $count1 = $this-&gt;mediamenumodel-&gt;getCountMediaMenu($where1);
          $getMediaMenu1 = $this-&gt;mediamenumodel-&gt;getMediaMenu($where1, $count1);
  
          $arr['media_menu_id']   = $value['media_menu_id'];
          $arr['media_menu_name'] = $value['media_menu_name'];
          $arr['media_menu_url']  = $value['media_menu_url'];
          $arr['thumbnail']       = !empty($value['thumbnail']) ? URL_CONTENT_MENU_THUMB.$value['thumbnail'] : "";
  
          if ($count1&gt;0) {
              $i=0;
              $arr['lewat'] = "ok";
              foreach ($getMediaMenu1 as $key1 =&gt; $value1){
                  $arr['submenu'][$i]['media_menu_id']    = $value1['media_menu_id'];
                  $arr['submenu'][$i]['media_menu_name']  = $value1['media_menu_name'];
                  $arr['submenu'][$i]['media_menu_url']   = $value1['media_menu_url'];
                  $arr['submenu'][$i]['thumbnail']        = !empty($value1['thumbnail']) ? URL_CONTENT_MENU_THUMB.$value['thumbnail'] : "";
  
  
                  $i++;
  
              }
  
          }
  
          $main_arrx[] = $arr;
          $h++;
      }
  
      $main_arr['data'] = $main_arrx;
  
      $json = json_encode($main_arr);
      echo $json;
  </code></pre>
  
  <p>can be sure my model is running well,
  i tried modify array format, but with no success</p>
  
  <p>and this is json the output:</p>
  
  <pre><code>{
  
  "rowCount": 5,
  "media_id": "1",
  "media_name": "sites",
  "data": [
      {
          "media_menu_id": "5",
          "media_menu_name": "Games",
          "media_menu_url": "",
          "thumbnail": "",
          "submenu": [
              {
                  "media_menu_id": "7",
                  "media_menu_name": "Games 2",
                  "media_menu_url": "",
                  "thumbnail": "",
                  "submenu": [
                      {
                          "media_menu_id": "9",
                          "media_menu_name": "Games 2-2",
                          "media_menu_url": null,
                          "thumbnail": ""
                      },
                      {
                          "media_menu_id": "8",
                          "media_menu_name": "Games 2-1",
                          "media_menu_url": null,
                          "thumbnail": ""
                      }
                  ]
              },
              {
                  "media_menu_id": "6",
                  "media_menu_name": "Games 1",
                  "media_menu_url": null,
                  "thumbnail": ""
              }
          ]
      },
      {
          "media_menu_id": "4",
          "media_menu_name": "Application",
          "media_menu_url": null,
          "thumbnail": "",
          "submenu": [
              {
                  "media_menu_id": "7",
                  "media_menu_name": "Games 2",
                  "media_menu_url": "",
                  "thumbnail": "",
                  "submenu": [
                      {
                          "media_menu_id": "9",
                          "media_menu_name": "Games 2-2",
                          "media_menu_url": null,
                          "thumbnail": ""
                      },
                      {
                          "media_menu_id": "8",
                          "media_menu_name": "Games 2-1",
                          "media_menu_url": null,
                          "thumbnail": ""
                      }
                  ]
              },
              {
                  "media_menu_id": "6",
                  "media_menu_name": "Games 1",
                  "media_menu_url": null,
                  "thumbnail": ""
              }
          ]
      },
      {
          "media_menu_id": "3",
          "media_menu_name": "Rich Content",
          "media_menu_url": null,
          "thumbnail": "",
          "submenu": [
              {
                  "media_menu_id": "7",
                  "media_menu_name": "Games 2",
                  "media_menu_url": "",
                  "thumbnail": "",
                  "submenu": [
                      {
                          "media_menu_id": "9",
                          "media_menu_name": "Games 2-2",
                          "media_menu_url": null,
                          "thumbnail": ""
                      },
                      {
                          "media_menu_id": "8",
                          "media_menu_name": "Games 2-1",
                          "media_menu_url": null,
                          "thumbnail": ""
                      }
                  ]
              },
              {
                  "media_menu_id": "6",
                  "media_menu_name": "Games 1",
                  "media_menu_url": null,
                  "thumbnail": ""
              }
          ]
      },
      {
          "media_menu_id": "2",
          "media_menu_name": "Top Download",
          "media_menu_url": null,
          "thumbnail": "",
          "submenu": [
              {
                  "media_menu_id": "7",
                  "media_menu_name": "Games 2",
                  "media_menu_url": "",
                  "thumbnail": "",
                  "submenu": [
                      {
                          "media_menu_id": "9",
                          "media_menu_name": "Games 2-2",
                          "media_menu_url": null,
                          "thumbnail": ""
                      },
                      {
                          "media_menu_id": "8",
                          "media_menu_name": "Games 2-1",
                          "media_menu_url": null,
                          "thumbnail": ""
                      }
                  ]
              },
              {
                  "media_menu_id": "6",
                  "media_menu_name": "Games 1",
                  "media_menu_url": null,
                  "thumbnail": ""
              }
          ]
      },
      {
          "media_menu_id": "1",
          "media_menu_name": "Hot Promo",
          "media_menu_url": "",
          "thumbnail": "",
          "submenu": [
              {
                  "media_menu_id": "10",
                  "media_menu_name": "Hot Promo 1",
                  "media_menu_url": null,
                  "thumbnail": "",
                  "submenu": [
                      {
                          "media_menu_id": "9",
                          "media_menu_name": "Games 2-2",
                          "media_menu_url": null,
                          "thumbnail": ""
                      },
                      {
                          "media_menu_id": "8",
                          "media_menu_name": "Games 2-1",
                          "media_menu_url": null,
                          "thumbnail": ""
                      }
                  ]
              },
              {
                  "media_menu_id": "6",
                  "media_menu_name": "Games 1",
                  "media_menu_url": null,
                  "thumbnail": ""
              }
          ]
      }
  ]
  
  }
  </code></pre>
  
  <p>and i want to my output like this:</p>
  
  <pre><code>{
  
  "rowCount": 5,
  "media_id": "1",
  "media_name": "sites",
  "data": [
      {
          "media_menu_id": "5",
          "media_menu_name": "Games",
          "media_menu_url": "",
          "thumbnail": "",
          "submenu": [
              {
                  "media_menu_id": "7",
                  "media_menu_name": "Games 2",
                  "media_menu_url": "",
                  "thumbnail": "",
                  "submenu": [
                      {
                          "media_menu_id": "9",
                          "media_menu_name": "Games 2-2",
                          "media_menu_url": null,
                          "thumbnail": ""
                      },
                      {
                          "media_menu_id": "8",
                          "media_menu_name": "Games 2-1",
                          "media_menu_url": null,
                          "thumbnail": ""
                      }
                  ]
              },
              {
                  "media_menu_id": "6",
                  "media_menu_name": "Games 1",
                  "media_menu_url": null,
                  "thumbnail": ""
              }
          ]
      },
      {
          "media_menu_id": "4",
          "media_menu_name": "Application",
          "media_menu_url": null,
          "thumbnail": ""
      },
      {
          "media_menu_id": "3",
          "media_menu_name": "Rich Content",
          "media_menu_url": null,
          "thumbnail": ""
      },
      {
          "media_menu_id": "2",
          "media_menu_name": "Top Download",
          "media_menu_url": null,
          "thumbnail": ""
      },
      {
          "media_menu_id": "1",
          "media_menu_name": "Hot Promo",
          "media_menu_url": "",
          "thumbnail": "",
          "submenu": [
              {
                  "media_menu_id": "10",
                  "media_menu_name": "Hot Promo 1",
                  "media_menu_url": null,
                  "thumbnail": ""
              }
          ]
      }
  ]
  </code></pre>
  
  <p>}</p>
  
  <p>thx,</p>