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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 12:16:281011browse

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

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

如何判断json数组中有多少json字符串?
------解决思路----------------------
如果传入数据的键名都是一样的,则

$s = '[{"answer":"123"},{"answer":"222"},{"answer":"231"}]';<br />$d = json_decode($s, true);<br />$t = join(',', array_map('current', $d));<br />$k = key(current($d));<br /><br />$sql = "select * from tbl_name where $k in ($t)";<br />
select * from tbl_name where answer in (123,222,231)

------解决思路----------------------
<br />$data = '[{"answer":"123"},{"answer":"222"},{"answer":"231"}]';<br />$data = json_decode($data, true);<br /><br />$answers = array();<br /><br />foreach($data as $val){<br />    array_push($answers, $val['answer']);<br />}<br /><br />$sqlstr = "select * from table where answer in('".implode("','", $answers)."')";<br />#mysql_query($sqlstr) or die(mysql_error());<br />echo $sqlstr; // select * from table where answer in('123','222','231')<br />

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