Php JSONArray解码并执行数据库操作

Php JSONArray解码并执行数据库操作

问题描述:

I'm sending a JsonArray from my android application as this:

   final JSONArray data = new JSONArray();

    try{
        for(int i = 0; i<contactsList.size(); i++){
            JSONObject jobj = new JSONObject();
            ObjectContacts ob = contactsList.get(i);
            jobj.put("contactid", ob.getContact_id());
            jobj.put("mobile", ob.getNumber());
            data.put(jobj);
        }

    } 

And the resulting Array that my server receive.

[
    {"contactid":"3","mobile":"(545) 454-5445"},
    {"contactid":"1","mobile":"(880) 939-5212"},
    {"contactid":"2","mobile":"822895456165"}
]

I need to fetch mobile numbers from this array and perform a Db Operation to look if this mobile number exist or not. How can I access each mobile and perform a Query? The query will consist of looking for mobile number existence and if it's true, it will fetch the name belonging to the mobile number and finally return an array back to the mobile application in JSONArray format which will consist of contact id, mobile, status(Yes or No), name.

Time won't be an issue but sometimes the array may contain 300-400 mobile number depending on user's contact.

Update

Here's the new Php Code that I implemented:

$app->post('/getcontacts', function () use ($app) {

//Verifying the required parameters
verifyRequiredParams(array('data'));

//Creating a response array
$response = array();


//reading post parameters
$data = $app->request->post('data');

$data_array = json_decode($data);


foreach ( $data_array as $obj ) {

 $res = array();
   $db = new DbOperation();
   $r = $db->checkContactExist($obj->mobile);
   if($r){
      $res["contactid"] = $obj->contactid;
      $res["mobile"] = $obj->mobile;
      $res["name"] = $obj->name;
      $res["status"] = 'yes';
      $res["image_small"] = $db->getImageSmall($obj->mobile);
   } else {
      $res["contactid"] = $obj->contactid;
      $res["mobile"] = $obj->mobile;
      $res["name"] = $obj->name;
      $res["status"] = 'no';
      $res["image_small"] = '';
   }

}

$response["error"] = false;
$response["message"] = json_encode($res);
echoResponse(201, $response);

});

The response I get from server:

{"contactid":"3","mobile":"(943) 101-9713","status":"no","image_small":""}

While there should be three contacts, it could only read one.

If I just send the incoming data back to application through echo to check whether all the contacts is coming or not, then it works correct. Maybe in the loop I'm adding detail about only one contact.

Second Update

Have solved the issue by putting :

$final_res = array();

and in foreach loop

$final_res[] = $res;

thus sending this back:

json_encode($final_res);

That string will convert to a PHP array of objects, after using json_decode() on the string.

So this is how you process it

<?php
$json_string = '[
    {"contactid":"3","mobile":"(545) 454-5445"},
    {"contactid":"1","mobile":"(880) 939-5212"},
    {"contactid":"2","mobile":"822895456165"}
]';

$json_array = json_decode($json_string);

foreach ( $json_array as $obj ) {
   echo $obj->contactid . ' - ' . $obj->mobile . PHP_EOL;
}

Result:

3 - (545) 454-5445
1 - (880) 939-5212
2 - 822895456165

You should be able to take this and add any database access around this simple code