PHP数组到json输出

时间:2016-04-13 13:00:36

标签: php json

我正在尝试创建一个json_encode函数,它输出如下的PHP数组:

{"userA":{"user":"userA","admin":true,"user_id":"000"}}

可悲的是,我只是使用我的PHP类得到以下输出:

{"user":"d4ne","admin":"true","user_id":"000"}

我的PHP课程:

<?php
    class add_mod_class {
        function __construct($username, $status){
            $this->username = $username;
            $this->status = $status;
            $this->user_id = '000';
            $this->json_file = 'includes/json/mods.json';
        }

        function get_json(){
            $json_content = file_get_contents($this->json_file);
            $json = json_decode($json_content, true);
            return $json;
        }

        function add_mod(){
            $mods = $this->get_json();
            array_push($mods, $data);

            $ds = array(
                'user' => $this->username,
                'admin' => $this->status,
                'user_id' => $this->user_id
            );

            $new_json_string = json_encode($ds);
            return $new_json_string;
        }

    }
?>

有没有人知道为什么我不能让它发挥作用?

修改

如果我编辑类以将代码推送到数组中,如:

<?php
    class add_mod_class {
        function __construct($username, $status){
            $this->username = $username;
            $this->status = $status;
            $this->user_id = '000';
            $this->json_file = 'includes/json/mods.json';
        }

        function get_json(){
            $json_content = file_get_contents($this->json_file);
            $json = json_decode($json_content, true);
            return $json;
        }

        function add_mod(){
            $mods = $this->get_json();

            $data[$this->username] = array(
                'user' => $this->username,
                'admin' => $this->status,
                'user_id' => $this->user_id
            );

            array_push($mods, $data);

            $new_json_string = json_encode($mods);
            return $new_json_string;
        }
    }
?>

然后输出如下所示:

{"swagg_ma_blue":{"user":"swagg_ma_blue","admin":true,"user_id":"000"},"0":{"d4ne":{"user":"d4ne","admin":"true","user_id":"000"}}}

其中包含"0":,其中有什么想法,有人有想法吗?

3 个答案:

答案 0 :(得分:0)

在add_mod()函数

中尝试以下代码
function add_mod(){
        $mods = $this->get_json();
        array_push($mods, $data);

        $ds[$this->username] = array( //added $this->username
            'user' => $this->username,
            'admin' => $this->status,
            'user_id' => $this->user_id
        );

        $new_json_string = json_encode($ds);
        return $new_json_string;
    }

在上面的代码中,我添加了$ this-&gt;用户名,以获得额外的数组,您需要额外的&#34; userA&#34;在数组中。并将产生以下结果。

{"userA":{"user":"userA","admin":true,"user_id":"000"}} 

答案 1 :(得分:0)

这对你没关系:

返回json_encode(数组($ this-&gt; username =&gt; $ ds));

答案 2 :(得分:0)

我真的不明白你是什么意思。如果你的意思是数组中的数组,你可以试试这个。

    $data = array( 'userA'=> array(
          'user' => $this->username,
          'admin' => $this->status,
          'user_id' => $this->user_id
        ));

//{"userA":{"user":"userA","admin":true,"user_id":"000"}}