我在将 PHP 中的数组转为 JSON 结构用于 Javascript 时遇到问题

发布于 2024-10-31 07:54:07 字数 1414 浏览 0 评论 0原文


我总是在数组到 JSON 格式方面遇到困难。我正在使用 TexoTela 的选择框插件。该插件需要 json 结构为:

{
    "ajax1": "AJAX option 1",
    "ajax2": "AJAX option 2",
    "ajax3": "AJAX option 3"
}  

我的 php 代码:

    function get_selectfield_list($col, $table)
    {
        $location_list = array();

        //create an sql string and set it to the $sql variable
        $sql = "SELECT id, $col FROM $table";

        //form the sql query and set it to the $query variable
        $query = $this->db->query($sql);

        //loop through the result_array and set the results to the $location_list array
        foreach ($query->result_array() as $row)
        {
            $value = $row['id']; //set the database table id to to $value variable
            $text = $row[$col]; //set the the $string value to the $text variable
            $location_list = array($value => $text); //form the $location_list array with the the $value and $text values
        }

        echo json_encode($location_list); //convert the $location_list array into a json object and return it.                              
    }  

我的 PHP 代码返回 {"4":"chickenpen 4"}

而不是 {"4":"chickenpen 4","5 ":"chickenpen 5" 等...}

我认为这是由于代码所致: $location_list = array($value => $text);每次 foreach 循环都会创建一个新数组。如何格式化数组以输出 foreach 中的所有结果而不是最后一个结果?

-富有的

I always struggle with Array to JSON formatting. I am using a selectbox plugin from TexoTela. The plugin requires a json structure as:

{
    "ajax1": "AJAX option 1",
    "ajax2": "AJAX option 2",
    "ajax3": "AJAX option 3"
}  

My php code:

    function get_selectfield_list($col, $table)
    {
        $location_list = array();

        //create an sql string and set it to the $sql variable
        $sql = "SELECT id, $col FROM $table";

        //form the sql query and set it to the $query variable
        $query = $this->db->query($sql);

        //loop through the result_array and set the results to the $location_list array
        foreach ($query->result_array() as $row)
        {
            $value = $row['id']; //set the database table id to to $value variable
            $text = $row[$col]; //set the the $string value to the $text variable
            $location_list = array($value => $text); //form the $location_list array with the the $value and $text values
        }

        echo json_encode($location_list); //convert the $location_list array into a json object and return it.                              
    }  

My PHP code returns {"4":"chickenpen 4"}

Instead of {"4":"chickenpen 4","5":"chickenpen 5", etc....}

I think that is due to the code: $location_list = array($value => $text); Every time the foreach loops it creates a new array. How do I format the array to output all the results in the foreach instead of the last result?

-Rich

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月打碎记忆 2024-11-07 07:54:07
    $location_list[$value] = $text;
    $location_list[$value] = $text;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文