0

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);
1
  • I tried going through a loop and then getting mobile number and performing a DB query. But the loop only ran once since the count(JSONArray) shows only 1 Commented Apr 23, 2016 at 17:08

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

9 Comments

What's . PHP_EOL here?
I tested this using the PHP CLI, you can replace that with <br> if you are using a browser for output. PHP_EOL is a PHP Constant that holds a NewLibe character that metches either Windows or Unix systems and give the right one for any of those options
Hi. I tried implementing your way but couldn't access all the row in arrays. Here's the php code $response = array(); $final_res = 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["status"] = 'yes'; $res["image_small"] = $db->getImageLarge($obj->mobile); } else { $res["contactid"] = $obj->contactid; $res["mobile"] = $obj->mobile; $res["status"] = 'no'; $res["image_small"] = ''; } $final_res["data"] = $res; }
$response["error"] = false; $response["message"] = json_encode($final_res); echoResponse(201, $response);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.