【问题标题】:CodeIgniter multidimensional array store in mysql database single columnCodeIgniter多维数组存储在mysql数据库单列中
【发布时间】:2017-11-12 12:34:30
【问题描述】:

我正在尝试将此数据存储在数据库中,但出现错误。如何解决这个问题?

我只想将多维数组存储在单列中。

$data = array(
            '2017' => array(
                '6' => array(
                    '10' => array(
                        'count' => 76
                    ),
                ),
            ),
        );

$getdata = $this->view_count->setView($data);

型号

public function setView($data)
    {
        $setData = $this->db->where('short', 'honwl')->update('ci_links', $data['view_count']);
        return $setData;
    }

我得到的错误 遇到 PHP 错误

严重性:通知

消息:未定义索引:view_count

文件名:models/View_count.php

行号:14

回溯:

文件:C:\wamp\www\blog\application\models\View_count.php 线路:14 函数:_error_handler

文件:C:\wamp\www\blog\application\controllers\Dashbord.php 线路:52 功能:设置视图

文件:C:\wamp\www\blog\index.php 线路:315 函数:require_once

发生数据库错误

您必须使用“set”方法来更新条目。

文件名:C:/wamp/www/blog/system/database/DB_query_builder.php

行号:1864

【问题讨论】:

    标签: php mysql codeigniter multidimensional-array


    【解决方案1】:

    正如消息所说,您没有密钥 $data['view_count'],但您有 $data[2017][6][10]['count'] 值。我假设这些日期是动态更改的,因此您需要通过键 count 获取内部数组的值。 如果您的数组总是有类似的键,即$data[year][month][day][count],您可以使用this answer 中的代码(修改位)来获取该键值。放入你的模型

    private function getCount($arr, $search)
    {
        $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr)); 
        foreach($iterator as $key => $value) {
            if($search == $key && $value !== '') {
                return $value;
            }
        }
        return false;
    }
    

    然后在你的第一个方法中使用通过这个函数过滤的值:

    public function setView($data)
    {
        $count = $this->getCount($data, 'count');
    
        if ($count !== false) {
            $setData = $this->db->where('short', 'honwl')->update('ci_links', $count);
            return $setData;
        }
        return false;
    }
    

    【讨论】:

    • $data = array('2017' => array('6' => array('10' => array('count' => 76),'11' => array(' count' => 42 ), '15' => 数组('count' => 23 ), ), ), );我只想将多维数组存储在单个列中。
    • 我好像回复了"Y" part of problem
    【解决方案2】:

    根据您的示例代码,您可以将数组数据编码为 JSON 以保存到列,然后通过解码返回:

    控制器:

    $data = array(
                '2017' => array(
                    '6' => array(
                        '10' => array(
                            'count' => 76
                        ),
                    ),
                ),
            );
    
    $getdata = $this->view_count->setView($data);
    $data = $this->view_count->getView();
    

    型号:

    public function setView($data)
    {
        $data = json_encode($data);
        $setData = $this->db->where('short', 'honwl')->update('ci_links', array('column_name'=>$data));
        return $setData;
    }
    
    public function getView($data)
    {
        $row = $this->db->where('short', 'honwl')->get('ci_links')->row();
        return json_decode($row->column_name, true);
    }
    

    column_name 取决于保存数组数据的表的列名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-12
      • 2011-09-12
      • 2016-12-07
      • 1970-01-01
      • 2015-11-24
      • 2018-02-21
      • 2015-09-22
      • 1970-01-01
      相关资源
      最近更新 更多