Reputation: 478
I'm trying to receive the data from JSON with PHP and use it in my SELECT query. I have searched everywhere and every solution didn't worked for me.
My Returned JSON data:
"{processos:[{"processo":"203"},{"processo":"1430"}]}"
My 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();
What's wrong with my code?
EDIT
The code from @wadersgroup worked correctly, but when i change to my variable it stops. This is the way i'm encoding it:
$jsonData = array();
$jsonData[] = array("processo" => $automovel->n_processo);
$data['processo2'] .= '{"processos":'.json_encode($jsonData).'}';
$data['processo2'] is sending to my AJAX to populate the input and then it receive data back with:
$processo2 = strip_tags(stripslashes($_POST['n_processo2']));
Upvotes: 2
Views: 152
Reputation: 727
$processo2 = '{"processos": [{ "processo": "203"},{ "processo": "1430"}]}'
$ar2 = json_decode($processo2, true);
$arr = array_map(function($i) {return $i['processo']; }, $ar2["processos"]);
$where = implode(", ", $arr);
Upvotes: 0
Reputation: 538
There are many errors in this code. Try this
$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);
Upvotes: 2
Reputation: 727
Your JSON string looks invalid, try instead:
'{"processos":[{"processo":"203"},{"processo":"1430"}]}'
Upvotes: 0