title: How to replace an existing json value with a new json?
body: <p>I want to compare every name of <code>$str1</code> with every name of <code>$str2</code>.<br>
  If a name in <code>$str1</code> is present in <code>$str2</code>, for example <code>'அடைமானம்'</code> is present in <code>$str2</code>, I want to replace the 
  <code>{"id":"3","name":"அடைமானம் ","parent":"1","tooltip":"   ","color":"#FF0000"}</code> from <code>$str1</code> with 3rd set.</p>
  
  <p><code>$str1</code> and <code>$str2</code> in pastebin:</p>
  
  <p><a href="https://pastebin.com/11p8U71x" rel="nofollow noreferrer">https://pastebin.com/11p8U71x</a></p>
  
  <pre><code>           &lt;?php
             $conn=mysqli_connect("localhost","root","pass","data");       
  
              if (mysqli_connect_errno())                
                {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
                }
               $name='அடகு';
              //To get the Main json
              $sql="SELECT * FROM mytab where value='$name' ";
  
              $r = mysqli_query($conn,$sql);
                      $data = array();
  
                      while($row = mysqli_fetch_assoc($r)) {
                        $data[] = $row;
                      }
              $nestedArray = array();
              foreach($data as $itemData){
  
                $nestedArrayItem['id'] = $itemData['id'];
                  $nestedArrayItem['name'] = $itemData['name'];
                  $nestedArrayItem['parent'] = $itemData['parent'];
                $nestedArrayItem['tooltip'] = $itemData['tooltip'];
                   $nestedArrayItem['color'] = $itemData['color'];
              $nestedArrayItem['children'] = array();
              insertIntoNestedArrayyy($nestedArray, $nestedArrayItem);
              }
              header('Content-Type: application/json');
                  $jj=(array_remove_empty($nestedArray));
              $json=json_encode($jj,JSON_UNESCAPED_UNICODE);
  
              echo "MAIN json:"."\n". $json;      
  
           echo "\n";
            echo "CHILD AND SUBCHILD JSON:"."\n";
              foreach($nestedArray as $r){
            foreach($r['children'] as $k){
            $w= $k['name'];
              //To get the subchild
           $sq="SELECT * FROM mytab where value='$w' ";
             $rr = mysqli_query($conn,$sq);
                      $dataa = array();
  
                      while($roww = mysqli_fetch_assoc($rr)) {
                        $dataa[] = $roww;
                      }
  
                $nestedArray = array();
              foreach($dataa as $itemData){
                $nestedArrayItem['id'] = $itemData['id'];
                  $nestedArrayItem['name'] = $itemData['name'];
                  $nestedArrayItem['parent'] = $itemData['parent'];
                $nestedArrayItem['tooltip'] = $itemData['tooltip'];
                   $nestedArrayItem['color'] = $itemData['color'];
                  $nestedArrayItem['children'] = array();
                  insertIntoNestedArrayyy($nestedArray, $nestedArrayItem);
               }
          $nestedArray=(array_remove_empty($nestedArray));
                      $c1= json_encode($nestedArray,JSON_UNESCAPED_UNICODE);
               $c1 = substr($c1, 1, -1);
             // $c1=str_replace("[]"," ",$c1);
  
             echo $c1;
  
              foreach($nestedArray as $r){
          foreach($r['children'] as $k){
          $w= $k['name'];
          //To get the sub-subchild
          $sql="SELECT * FROM mytab where value='$w' ";
                      $rr = mysqli_query($conn,$sql);
                      $dataa = array();
  
                      while($roww = mysqli_fetch_assoc($rr)) {
                          if($roww &gt;0){
                        $dataa[] = $roww;
                  }}
  
                 $nestedArray = array();
              foreach($dataa as $itemData){
                $nestedArrayItem['id'] = $itemData['id'];
                  $nestedArrayItem['name'] = $itemData['name'];
                  $nestedArrayItem['parent'] = $itemData['parent'];
                $nestedArrayItem['tooltip'] = $itemData['tooltip'];
                   $nestedArrayItem['color'] = $itemData['color'];
                  $nestedArrayItem['children'] = array();
                  insertIntoNestedArrayyy($nestedArray, $nestedArrayItem);
                }
          $nestedArray=(array_remove_empty($nestedArray));
                      $c2= json_encode($nestedArray,JSON_UNESCAPED_UNICODE);
                      $c2=str_replace("[]"," ",$c2);
  
              echo $c2."\n";
                }
  
  
                   }
  
           }
            }
            function buildtt($src_arr, $parent_id = 0, $tree = array())
              {
                  foreach($src_arr as $idx =&gt; $roww)
                  {
                      if($roww['parent'] == $parent_id)
                      {
                          foreach($roww as $k =&gt; $v)
                              $tree[$roww['id']][$k] = $v;
                          unset($src_arr[$idx]);
              $tree[$roww['id']]['children'] = buildtt($src_arr, $roww['id']);
                      }
                  }
                  ksort($tree);
                  return $tree;
              }
               function array_remove_empty($haystack)
              {
                  foreach ($haystack as $key =&gt; $value) {
                      if (is_array($value)) {
                      $haystack[$key] = array_remove_empty($haystack[$key]);
                      }
  
                      if (empty($haystack[$key])) {
                          unset($haystack[$key]);
                      }
                  }
  
                  return $haystack;
              }
              function insertIntoNestedArrayyy(&amp;$array, $searchItem){
  
                  if($searchItem['parent'] == 0){
                      array_push($array, $searchItem);
                      return;
                  }
                  if(empty($array)){ return; }
               array_walk($array, function(&amp;$item, $key, $searchItem){
                  if($item['id'] == $searchItem['parent']){
                          array_push($item['children'], $searchItem);
                          return;
                      }
                      insertIntoNestedArrayyy($item['children'], $searchItem);
              }, $searchItem);
              }
              ?&gt;
  </code></pre>