Recover array generated in JSON and sent by POST

2

I create a array with JSON and send it in an encrypted variable in base64 in a form. I recover it by POST , the decrypted and it remains:

[
 {"idcliente":"1",
  "id_comercial":"999999",
  "preciototal":"698.01",
  "payTotalTotal":"713.01"
 }
]

How can I get each element of array apart so I can store it in a column in mysql each? Thanks

    
asked by rafa_pe 27.06.2016 в 19:13
source

2 answers

3

to get the post

$dataJson = $_POST['datos']

You can use json_decode(datos) you will get an associative array with the structure of your json.

$arrayDatos = json_decode($dataJson)

Example:

$datosJson = <<<EOF

[
 {"idcliente":"1",
  "id_comercial":"999999",
  "preciototal":"698.01",
  "payTotalTotal":"713.01"
 }
]

EOF;

$arrayJson = json_decode($datosJson);

print_r($arrayJson);

To traverse the array for each node

foreach ($arrayJson[0] as $key => $value) {
    echo $key . ' = ' . $value . '<br />';  
}

Result

idcliente = 1
id_comercial = 999999
preciototal = 698.01
payTotalTotal = 713.01
    
answered by 27.06.2016 / 19:18
source
2
<?php

$datosJson = <<<EOF
[
 {"idcliente":"1",
  "id_comercial":"999999",
  "preciototal":"698.01",
  "payTotalTotal":"713.01"
 }
]
EOF;

$arrayJson = json_decode($datosJson);

// Asi imprimirias o cojerias el valor del id_comercial
$id_comercial =  $arrayJson[0]->id_comercial;
echo $id_comercial;
?>
    
answered by 28.06.2016 в 08:02