我试图用PHP从JSON接收数据并在SELECT查询中使用它。我到处寻找,但每一个解决方案都不适合我。
我返回的JSON数据:

"{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['processo2']正在发送到我的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);

09-20 23:37