Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 不断获得;数组";从获取解码的JSON数据?_Php_Mysql_Arrays_Json_Mysqli - Fatal编程技术网

Php 不断获得;数组";从获取解码的JSON数据?

Php 不断获得;数组";从获取解码的JSON数据?,php,mysql,arrays,json,mysqli,Php,Mysql,Arrays,Json,Mysqli,当我对从mysql数据库获取的json数据进行编码时,我得到如下结果: [{“userid”:“11”,“postid”:“12”},{“userid”:“13”,“postid”:“12”},{“userid”:“16”,“postid”:“12”}] 我试图只获取这样的用户ID:11,13,16 对json数据进行解码会得到以下输出:Array。没有别的了。当我var\u dump这是我的结果: array(3) { [0]=> array(2) {

当我对从mysql数据库获取的json数据进行编码时,我得到如下结果:

[{“userid”:“11”,“postid”:“12”},{“userid”:“13”,“postid”:“12”},{“userid”:“16”,“postid”:“12”}]

我试图只获取这样的用户ID:
11,13,16

对json数据进行解码会得到以下输出:
Array
。没有别的了。当我
var\u 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数据时,这有什么原因吗?

尝试映射这些结果:

$new_postid = $decode_postid;
$new_postid = array_map(function($item){
    return $item['userid'];
}, $new_postid);

var_dump($new_postid);
输出

阵列(3){ [0]=> 字符串(2)“11” => 字符串(2)“13” [2]=> 字符串(2)“16” }

若您需要输出为字符串,只需使用函数


尝试映射这些结果:

$new_postid = $decode_postid;
$new_postid = array_map(function($item){
    return $item['userid'];
}, $new_postid);

var_dump($new_postid);
输出

阵列(3){ [0]=> 字符串(2)“11” => 字符串(2)“13” [2]=> 字符串(2)“16” }

若您需要输出为字符串,只需使用函数


您是否试图从die函数中取出该数组?它只能接受字符串或int

你的阵列在那里。。。只需做如下操作:

$ids = '';
foreach ($new_postid as $post) {
    $ids .= $post['userid'] . ',';
}
$ids = rtrim($ids,',');
die($ids);

你是想从骰子函数中取出那个数组吗?它只能接受字符串或int

你的阵列在那里。。。只需做如下操作:

$ids = '';
foreach ($new_postid as $post) {
    $ids .= $post['userid'] . ',';
}
$ids = rtrim($ids,',');
die($ids);

$new\u postid
在一个while循环中被加载,但是每次循环中你都写得太多了
$new\u postid
在一个while循环中被加载,但是每次循环中你都写得太多了谢谢你,这就是我得到的,但是我期待着
die($new\u postid)
:11、13、16的结果。只有号码没问题。在我看来,使用语言结构更好。代码变得更干净,更容易维护。如果你有更好的方法避免子字符串和循环,我同意!同时,我计划将数字转换为名称。例如,用“比利”代替“11”。我必须想办法用php查询表来实现这一点。我还有一个问题,比如我是否也想要
postid
。我会在这里做同样的事情,只是更改变量名吗?谢谢,这就是我得到的,但我希望在
die($new\u postid)
中得到这些结果:11,13,16。只有号码没问题。在我看来,使用语言结构更好。代码变得更干净,更容易维护。如果你有更好的方法避免子字符串和循环,我同意!同时,我计划将数字转换为名称。例如,用“比利”代替“11”。我必须想办法用php查询表来实现这一点。我还有一个问题,比如我是否也想要
postid
。我会在这里做同样的事情,只是改变变量名吗?是的,这就是我要找的!非常感谢你!是的,这就是我要找的!非常感谢你!