我试图从PHP接收JSON的数据,并在我的SELECT查询中使用它。我到处都找过了,但每种方法都不适合我。
My return JSON data:
"{processos:[{"processo":"203"},{"processo":"1430"}]}"
我的PHP:
$ar2 = json_decode($processo2, true);
$where2 = array();
foreach($ar2['processo'] as $key=>$val){
$where2[] = $val;
}
$where2 = implode(',', $where2);
$pegaSonda = $pdo->prepare("SELECT * FROM sonda WHERE n_processo IN ($where2)");
$pegaSonda->execute();
我的代码有什么问题?
编辑
代码从@wadersgroup工作正常,但当我改变到我的变量它停止。这是我的编码方式:
$jsonData = array();
$jsonData[] = array("processo" => $automovel->n_processo);
$data['processo2'] .= '{"processos":'.json_encode($jsonData).'}';
$data[' processso2 ']发送给我的AJAX填充输入,然后它接收数据返回:
$processo2 = strip_tags(stripslashes($_POST['n_processo2']));
这段代码有很多错误。试试这个
$ar2 = json_decode('{"processos":[{"processo":"203"},{"processo":"1430"}]}', true);
$where2 = array();
foreach($ar2['processos'] as $key=>$val){
$where2[] = $val['processo'];
}
$where = implode(',', $where2);
print_r($where);
您的JSON字符串看起来无效,请尝试:
{"processos":[{processo ": " 203 "}, {" processo ": " 1430 "}]}"
$processo2 = '{"processos": [{ "processo": "203"},{ "processo": "1430"}]}'
$ar2 = json_decode($processo2, true);
$arr = array_map(function($i) {return $i['processo']; }, $ar2["processos"]);
$where = implode(", ", $arr);