2

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']));
5
  • exactly HOW is this not working for you? wrong data returned? no data returned? errors thrown? insults issued? dogs kicked? Commented Jul 8, 2015 at 18:57
  • @MarcB, in $where2 i'm not receiving the correct data, it was supossed to call a second database table Commented Jul 8, 2015 at 19:00
  • $ar2 = json_decode($processo2, true); maybe a typo to $ar2 = json_decode($processos, true); Commented Jul 8, 2015 at 19:01
  • @vinayakj, $processo2 is the input field that's sending the data to PHP. Commented Jul 8, 2015 at 19:06
  • then your root becomes ar2 not $processos, so try $ar2.processos['processo'] Commented Jul 8, 2015 at 19:09

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

10 Comments

your code worked like a charm, but with my variable is not working, maybe the way i'm encoding?
After encoding no character under the string should violet the JSON schema
I have updated my question with the json_encode code, can you check to see if there's something wrong please?
TRY IT $jsonData = array(); $jsonData[] = array("processo" => json_encode($automovel->n_processo)); $data['processo2'] .= '{"processos":'.$jsonData.'}';
It returns "{"processos":Array}"
|
0

Your JSON string looks invalid, try instead:

'{"processos":[{"processo":"203"},{"processo":"1430"}]}'

2 Comments

I've tried that, now it's returning "{"processos":[{"processo":"203"},{"processo":"1430"}]}" but still don't work.
so now try $ar2.processos['processo'] as $key=>$val
0
$processo2 = '{"processos": [{ "processo": "203"},{ "processo": "1430"}]}'
$ar2 = json_decode($processo2, true);    
$arr = array_map(function($i) {return $i['processo']; }, $ar2["processos"]);
$where = implode(", ", $arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.