title: Create an object and add it to json to save a file
body: <p>I have a <code>.json</code> file and I want to append an object to a vector in the file, but I got some errors.</p>
  
  <pre><code>            $json = file_get_contents('materialsdb/'.$_POST["materialtype"].'.json');
              $jsonarray = json_decode($json, true);
  
              $myObj-&gt;short = $_POST["short"];
  //here I got: Warning: Creating default object from empty value
              $myObj-&gt;title = $_POST["title"];
              $myObj-&gt;description = $_POST["description"];
              $myObj-&gt;cover = "";
              $myObj-&gt;VR = false;
              $myObj-&gt;slow = false;
              $td = '2D';
              $myObj-&gt;$td = false;
              $fd = '3D';
              $myObj-&gt;$fd = false;
              if($_POST["isprivate"]){
                  $myObj-&gt;license = "nonfree";
              } else {
                  $myObj-&gt;license = "free";
              }
              $myObj-&gt;lang = "en";
              $id = count($jsonarray['packages'])+1;
              $myObj-&gt;id = $id;
              $myObj-&gt;soon = false;
              $myObj-&gt;date = $_POST["date"];
              $myObj-&gt;creator = $_POST["creator"];
              $myObj-&gt;creator_institution = $_POST["creator_institution"];
              $myObj-&gt;keywords = $_POST["keywords"];
              $myObj-&gt;data = $_POST["data"];
  
              $myJSON = json_encode($myObj);
              echo $myJSON;
              array_push($myJSON,$jsonarray['packages']);
  //here I got: Warning: array_push() expects parameter 1 to be array, string given
              $jsondata = json_encode($jsonarray, true);
  
              $myFile = 'materialsdb/'.$_POST["materialtype"].'.json';
              if(file_put_contents($myFile, $jsondata)) {
                  echo 'Data successfully saved';
              } else 
                  echo "error";
  </code></pre>
  
  <p>And when I try to save it then It is saved, but without the modifications, without the new object, but where I <code>echo $myJSON</code> there the object seems good.</p>
  
  <p>Here is an example of my <code>.json</code> file:</p>
  
  <pre><code>{
    "description": "some description",
    "creators": [
      {
        "name": "cname",
        "whoishe": "cv",
        "avatar": "",
        "id": 123
      }
    ],
    "lang": "en",
    "materialname": "mat name",
    "generalmaterialid": "mat_id",
    "thismaterialid": "this_mat_id",
    "packages": [
      {
        "short": "pack short desc",
        "title": "pack title",
        "description": "pack long desc",
        "cover": "pack cover",
        "VR": true,
        "slow": true,
        "2D": true,
        "3D": true,
        "license": "free",
        "lang": "en",
        "id": 1,
        "soon": false
      }
    ]
  }
  </code></pre>
  
  <p>I have been inspired from here: <a href="https://www.w3schools.com/js/js_json_php.asp" rel="nofollow noreferrer">https://www.w3schools.com/js/js_json_php.asp</a> and here <a href="http://www.kodecrash.com/javascript/read-write-json-file-using-php/" rel="nofollow noreferrer">http://www.kodecrash.com/javascript/read-write-json-file-using-php/</a>.</p>
  
  <p>What have I done wrong here? How can I resolve it? What is the correct version in this case? Thanks for any help!</p>