php - json_decode,尝试访问数组元素

标签 php mysql json object

$objetos = json_decode($_POST['objetos']);

$query1 = "DELETE FROM `usuarioObjeto` WHERE idusuario=" . $id . "";
$result1 = mysqli_query($conn, $query1) or die('Consulta fallida: ' . mysqli_error());



$size = count($objetos); //this  works

//这个不要插入到BD中

for ($k = 0; $k < $size; $k++) {
    $ido = intval($objetos[$k]['id']);
    $cantidad = intval($objetos[$k]['cantidad']);
    $query2 = "INSERT INTO `usuarioObjeto`( `idUsuario`, `idObjeto`, `cantidad`) VALUES (" . $id . "," . $ido . "," . $cantidad . ")";
    $result2 = mysqli_query($conn, $query2) or die('Consulta fallida: ' . mysqli_error());
}

谢谢

我尝试访问这样的一个属性,但什么也没有

$ido = intval($objetos[0]['id']); 

最佳答案

json_decode($_POST['objetos']); 替换 json_decode($_POST['objetos'],true);

通过添加 true 作为第二个参数,它将把您的 json 转换为 array

更多信息:json_decode

关于php - json_decode,尝试访问数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35149695/

相关文章:

mysql - 多表join MySQL多外键

c# - 条件从使用 LEFT JOIN 的查询中排除行

json - Laravel 5.1自动完成功能给Symfony fatal error

php - 数据库 : Fetch two fields from same column on every array result

php -/dev/urandom 错误(网络主机拒绝许可)

php - 不删除记录

php - 如何从 Mysql 访问 firebird?

ios - JSON 对象为具有值的键返回空字符串

python - Web2Py 应用程序在本地计算机上运行,​​但不在 PythonAnywhere 上运行

php - 您如何构造 URL 来访问 google docs/drive 文件夹?