Codeigniter - 它可能在单个mysql列中存储多维数组数据?

时间:2017-06-11 06:45:12

标签: php mysql codeigniter multidimensional-array

多维数组数据存储在单个mysql列中。

这是我的数据。如何将它存储在单个mysql列中?

有人告诉我带有示例代码的解决方案。

$data = array(
        '2017' => array(
            '6' => array(
                '10' => array(
                    'count' => 76
                ),
                '11' => array(
                    'count' => 42
                ),
                '15' => array(
                    'count' => 23
                ),
            ),
            '7' => array(
                '5' => array(
                    'count' => 26
                ),
                '25' => array(
                    'count' => 82
                ),
                '26' => array(
                    'count' => 53
                ),
            ),
        ),
        '2018' => array(
            '6' => array(
                '18' => array(
                    'count' => 30
                )
            )
        ),
    );

3 个答案:

答案 0 :(得分:1)

让您的专栏输入文字以获取更长的数据。 然后使用

$data = json_encode($data);

然后存储$ data。

后来,得到它。只需使用decode:

$data = json_decode($data); // Alternately json_decode($data,true);

答案 1 :(得分:0)

您也可以serialize数组到字符串来保存数据库:

$stringData = serialize($data);

然后你可以unserialize回到数组:

$data = unserialize($stringData );

答案 2 :(得分:0)

serialize将PHP数组或对象转换为字符串。 unserialize将该字符串转换为存储为的字符串。