Read data from an array that is inside another array

0

I have an associative array (data) that contains data and another associative array (other values) within it, the problem is that I can only read the data contained before the array (other values) throwing the error Catchable fatal error: Object of class stdClass could not be converted to string this in php, if I print it by console everything is correct, what I want is to be able to access the values of the array (other values);
Jquery

    var otrosValores={
        val1:"valor 1",
        val2:"valor 2",
        val3:"valor 3"
    }
    var dato={
        nombre:"aqui el nombre",
        apellido:"aqui el apellido",
        rut:"aqui el rut",
        otros:otrosValores,
    }
    console.dir(dato)
    $.ajax({
        url : 'admin/controlador.php',
        type : 'POST',
        cache : false,
        data : "op=saveFicha&datos="+JSON.stringify(dato),
        success: function(resultado){
            $(".resultado").html(resultado)                 
        }
    })

Php

$datos=json_decode($_POST["datos"]);

foreach ($datos as $key => $value) {
    echo $key," => ",$value,"<br>";
    if($key=="otros"){
        foreach ($key as $key2 => $value2) {
            echo $key2," => ",$value2;
        }
    }
}
    
asked by Kevin 16.10.2017 в 22:21
source

1 answer

0

The problem is that in javascript you are not declaring an associative array, you are actually declaring an object. So, when making the decode in php you are not really getting an array, but an object. I think it would solve your problem if you access the variable $datos with the operator -> . If you want to access as if it were an array I think that the get_object_vars($objeto) function will make the conversion between object and array that you are looking for.

You can see more info about this feature at: link

    
answered by 16.10.2017 в 22:42