dpyln64620 2016-02-02 09:38
浏览 21
已采纳

json_decode,试图访问数组元素

$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

//this do not insert into the 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());
}

thanks

and i have tried to access to one property like this but nothing

$ido = intval($objetos[0]['id']); 
  • 写回答

2条回答 默认 最新

  • dongwalun2507 2016-02-02 09:46
    关注

    json_decode($_POST['objetos']); replece to json_decode($_POST['objetos'],true);

    By adding true as a second parameter, it will convert your json to array

    More information: json_decode

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法