【问题标题】:Convert PHP Array to Json. Using Code-igniter将 PHP 数组转换为 Json。使用代码点火器
【发布时间】:2016-04-30 11:01:47
【问题描述】:

我将 PHP 数组转换为 JSON 成功,但我的目标未满。

我的输出是:

{"id":"145","logid":"","name":"asasa"}{"id":"144","logid":"","name":"qwsqwswq"}{"id":"145","logid":"","name":"asasa"}{"id":"144","logid":"","name":"qwsqwswq"}

我的要求:

[{"id":"145","logid":"","name":"asasa"}{"id":"144","logid":"","name":"qwsqwswq"}{"id":"145","logid":"","name":"asasa"}{"id":"144","logid":"","name":"qwsqwswq"}]

这是我的代码:-

if ($AdminRecord) {

                    foreach($AdminRecord->result() as $results)
                    {
                      echo json_encode($results);
                    }
                }

【问题讨论】:

    标签: php json codeigniter


    【解决方案1】:

    你正在输出每一行,试试这个:

    $resultSet = Array();
    if($AdminRecord){
        foreach($AdminRecord->result() as $result) {
           $resultSet[] = $result;
        }
    }
    echo json_encode($resultSet);
    

    【讨论】:

    • 那是伟大的 R Singh,धन्यवाद
    • @AnshuPratapYadav,如果它解决了您的问题,请将其更改为已接受的答案。
    • 运行良好,感谢 R Singh。@
    猜你喜欢
    • 2011-06-04
    • 2011-11-15
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    相关资源
    最近更新 更多