当我对从mysql数据库获取的json数据进行编码时,我得到了:
[{"userid":"11","postid":"12"},{"userid":"13","postid":"12"},{"userid":"16","postid":"12"}]
我正在尝试只获取像这样的userid:11, 13, 16
。
对json数据进行解码得到的输出是:Array
。没别的了。当我使用var_dump
时,我的结果如下:
array(3) {
[0]=> array(2) {
["userid"]=> string(2) "11"
["postid"]=> string(2) "12"
}
[1]=> array(2) {
["userid"]=> string(2) "13"
["postid"]=> string(2) "12"
}
[2]=> array(2) {
["userid"]=> string(2) "16"
["postid"]=> string(2) "12"
}
所以我知道有数据存在,只是由于某些原因没有显示出来。
下面是我的查询:
if(isset($_POST['postuserid'])){
$uid = $_POST['postuserid'];
$ssql = "SELECT * FROM foodid WHERE postid=$uid";
$rresult = mysqli_query($db,$ssql);
while ($lrow = mysqli_fetch_assoc($rresult)){
$ret[] = $lrow;
$postjson = json_encode($ret);
$decode_postid = json_decode($postjson, true);
$new_postid = $decode_postid;
}
var_dump($new_postid);
die($new_postid);
// die($new_postid['userid']); is what I need along with all data userid fetched.
}
当我编码json数据而不是解码json数据时,这有什么原因吗?
发布于 2018-07-11 05:56:07
您是否正在尝试将该数组从die函数中取出?它只能接受字符串或int。
http://php.net/manual/en/function.exit.php
你的数组就在那里...只要像这样做就行了:
$ids = '';
foreach ($new_postid as $post) {
$ids .= $post['userid'] . ',';
}
$ids = rtrim($ids,',');
die($ids);
https://stackoverflow.com/questions/51274340
复制相似问题