php decode json received via _POST

1

I have the following case: I receive a variable _POST that brings two data in JSON format and I need only take one of those data. I try this, but the result is NULL. Any suggestions? Thank you very much

<?php
    $CuentasBancariasSel=$_POST["CuentasBancariasSel"]; //Recibo: array(1) { [557]=> string(33) "{'id_cuenta':'1','id_banco':'39'}" } 
    $obj = json_decode($CuentasBancariasSel);
    $CuentasBancariasSelOK = $obj->{'id_cuenta'};
?>
    
asked by pointup 20.11.2018 в 03:09
source

1 answer

1

I found the solution. I leave the code: I should parsed the array and replace the single quotes with doubles.

Suggestion: Be always attentive to the quotes because they give a lot of headache. Greetings!

<?php
$CuentasBancariasSel=$_POST["CuentasBancariasSel"];
foreach($CuentasBancariasSel as $CuentasBancarias){
$arr=str_replace("'","\"",$CuentasBancarias);	
$obj = json_decode($arr);
$CuentaBancaria = $obj->{"id_cuenta"};
}
?>
    
answered by 20.11.2018 в 15:22