title: How to convert a multidimensional associative array to JSON?
body: <p>As you can see in the <code>JSON</code> file, this looks not so necessary.
  I get values from input-s and add it to the array and send it via <code>AJAX.</code> A simple array I know how to convert, but a multidimensional one is not. Can eat what that function? I tried to create an array with "keys", but there is a lot of trouble, I never reached the end , and I'm sure it's not right. Tell me what you can do.</p>
  
  <pre><code>i want this
  {   
     "user1" : {
        first_name":"Michael",
        "last_name":"Podlevskykh",
         "phones" : {
             "phone_1":"5345",
              "phone_2":"345345",
              "phone_3":"123"
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code> //this is what i see
  JSON
  [
      {"first_name":"Michael"},
      {"last_name":"Podlevskykh"},
          [{"phone_1":"5345"},
              {"phone_2":"345345"},
              {"phone_3":"0991215078"}
          ]
  ]
  </code></pre>
  
  <hr>
  
  <pre><code>PHP
  
  //[["5345", "345345", "123"], "Michael", "Podlevskykh"]
  
  $userInfo = (json_decode($_POST["phones"], true));
  
  $namePhones = ["phone_1", "phone_2", "phone_3"];
  $nameUser = ["first_name", "last_name"];
  $jsonPhones = $userInfo;
  $nameLName = $userInfo;
  
  $jsonPhones = array_splice($jsonPhones, 0, 1);
  $nameLName = array_splice($nameLName, -2);
  
  foreach ($jsonPhones[0] as $key =&gt; $value) {
      $phones[] = array($namePhones[$key] =&gt; $jsonPhones[0][$key]);
  }
  
  foreach ($nameLName as $key =&gt; $value) {
      $usersName[] = array($nameUser[$key] =&gt; $nameLName[$key]);
  }
  
  array_push($usersName, $phones);
  
  echo "&lt;pre&gt;";
  echo json_encode($usersName);
  //[ 
   // {"first_name":"Michael"},{"last_name":"Podlevskykh"},
  //  [{"phone_1":"5345"},{"phone_2":"345345"},{"phone_3":"123"}]
  //]
  </code></pre>