-2

I want to send a json from a js script to a php file but when the json is recieved in the php file echo just shows "Array"

    var tot = [];
    for (var i = 0; i < con.length; i++) {
      //tot = tot +"x" + num[i].value +":"+ con[i].textContent + ",";
      tot.push({id:id[i].textContent, nombre : con[i].textContent, cantidad:num[i].value});

    }


    $.ajax({
      type:"POST",
      data : {json:JSON.stringify(tot)},
      url:direccion,
      success:function(data){
        if(data=="bien"){
            swal("Se ha realizado el pago exitosamente");
        }else{
            alert(data)
        }
      }

PHP code

$n = json_decode($_POST['json']);
echo ($n);
  • 1
    It is your fault, not PHP. `Use var_dump($n);`. – Markus Zeller Mar 23 '21 at 20:15
  • Does this answer your question? [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – El_Vanja Mar 23 '21 at 20:30
  • @MarkusZeller This problem in that question was that they were trying to echo a nested element and they got an index wrong. Totally different from this. – Barmar Mar 23 '21 at 20:30

1 Answers1

1

That's what happens when you try to echo() any array. echo() is for scalar values. If you want to see the values and structure of an array, use print_r()

Quasipickle
  • 4,383
  • 1
  • 31
  • 53