在做一个ios端接口,对方传来多个json字符串,我应该如何将json数据与数据库数据做匹配?
我的想法是先根据ios传来的json数组来判断有多少个json字符串,然后进行循环匹配。不知方法是否正确。
如何判断json数组中有多少json字符串?
------解决思路----------------------
如果传入数据的键名都是一样的,则
$s = '[{"answer":"123"},{"answer":"222"},{"answer":"231"}]';select * from tbl_name where answer in (123,222,231)
$d = json_decode($s, true);
$t = join(',', array_map('current', $d));
$k = key(current($d));
$sql = "select * from tbl_name where $k in ($t)";
------解决思路----------------------
$data = '[{"answer":"123"},{"answer":"222"},{"answer":"231"}]';
$data = json_decode($data, true);
$answers = array();
foreach($data as $val){
array_push($answers, $val['answer']);
}
$sqlstr = "select * from table where answer in('".implode("','", $answers)."')";
#mysql_query($sqlstr) or die(mysql_error());
echo $sqlstr; // select * from table where answer in('123','222','231')