title: Passing JSONArray to PHP from Android
body: <p>I have read through many other questions regarding PHP and JSONArray looping. I am sending in a JSONArray of values of different students with their class and studentId from my Android device. Using these values, I search the database for their names and return a JSONArray.</p>
  
  <pre><code>JSON Input: [{"studentId":"2","class":"2a","dbname":"testDb"}]
  &lt;?php
  
   $jsonString = $_POST['json'];  //see comment below
  
   $jArray = json_decode($jsonString, true);
  
   $conn = mysql_connect('localhost', 'user', 'pwd' );
  
  mysql_select_db('dbname', $conn);
  
  foreach( $jArray as $obj ){
      $className = $obj['class'];   //String
      $id= $obj['studentId'];       //int
  
      $result = mysql_query("SELECT name FROM student WHERE class='$className' AND id='$id'");
  
      $e=mysql_fetch_assoc($result);    //will only fetch 1 row of result
      $output[]=$e;
  
  
  }
  
        echo (json_encode($output));
  
  ?&gt;
  </code></pre>
  
  <p>Android</p>
  
  <pre><code>HttpClient client = new DefaultHttpClient();
  HttpResponse response;
  try{
   HttpPost post = new HttpPost("http://abc/getName.php");
   List&lt;NameValuePair&gt; nVP = new ArrayList&lt;NameValuePair&gt;(2);  
   nVP.add(new BasicNameValuePair("json", studentJson.toString()));  //studentJson is the JSON input
  
  //student.Json.toString() produces the correct JSON [{"studentId":"2","class":"2a","dbname":"testDb"}]
  
   post.setEntity(new UrlEncodedFormEntity(nVP));
   response = client.execute(post);
  if(response!=null){
  //process data send from php
  }  
  }
  </code></pre>
  
  <p><strong>SOLVED:</strong> See answer below</p>