Home >Backend Development >PHP Tutorial >在做一个ios端接口,对方传来多个json字符串,我应该如何将json数据与数据库数据做匹配?

在做一个ios端接口,对方传来多个json字符串,我应该如何将json数据与数据库数据做匹配?

WBOY
WBOYOriginal
2016-06-23 13:38:20798browse

在做一个ios端接口,对方传来多个json字符串,我应该如何将json数据与数据库数据做匹配?

我的想法是先根据ios传来的json数组来判断有多少个json字符串,然后进行循环匹配。不知方法是否正确。

如何判断json数组中有多少json字符串?


回复讨论(解决方案)

先看看都是什么样的数据

??格式是怎?的?

正常json_decode後????可以解???了。

数据格式是 [{"answer":"123"},{"answer":"222"},{"answer":"231"}]

数据格式是 [{"answer":"123"},{"answer":"222"},{"answer":"231"}]



把得到的这个json字符串 json_decoe()一下 就得到数组形式了。

如果传入数据的键名都是一样的,则

$s = '[{"answer":"123"},{"answer":"222"},{"answer":"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)";
select * from tbl_name where answer in (123,222,231)

$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')

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn