Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter多维数组存储在mysql数据库单列中_Php_Mysql_Codeigniter_Multidimensional Array - Fatal编程技术网

Php CodeIgniter多维数组存储在mysql数据库单列中

Php CodeIgniter多维数组存储在mysql数据库单列中,php,mysql,codeigniter,multidimensional-array,Php,Mysql,Codeigniter,Multidimensional Array,我试图将此数据存储在数据库中,但出现错误。如何解决这个问题 我只想将多维数组存储在一列中。 $data = array( '2017' => array( '6' => array( '10' => array( 'count' => 76 ), ),

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

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

$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错误

严重性:通知

消息:未定义索引:查看\u计数

文件名:models/View_count.php

电话号码:14

回溯:

文件:C:\wamp\www\blog\application\models\View\u count.php 第14行 函数:\u错误\u处理程序

文件:C:\wamp\www\blog\application\controllers\Dashbord.php 电话号码:52 功能:setView

文件:C:\wamp\www\blog\index.php 电话号码:315 功能:需要一次

发生数据库错误

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

文件名:C:/wamp/www/blog/system/database/DB\u query\u builder.php


行号:1864

正如消息所说,您没有键
$data['view\u count']
,但您有
$data[2017][6][10]['count']
值。我估计这些日期是动态更改的,所以您需要通过键
count
获取内部数组的值。 如果数组始终具有类似的键,即
$data[year][month][day][count]
,则可以使用中的代码(位修改)来获取该键值。把你的模型放进去

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['view_count']
,但您有
$data[2017][6][10]['count']
值。我估计这些日期是动态更改的,所以您需要通过键
count
获取内部数组的值。 如果数组始终具有类似的键,即
$data[year][month][day][count]
,则可以使用中的代码(位修改)来获取该键值。把你的模型放进去

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;
}

根据您的示例代码,您可以将数组数据编码为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);
}
型号:

$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);
}

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

根据示例代码,您可以将数组数据编码为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);
}
型号:

$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\u name
取决于表中保存数组数据的列名。

$data=array('2017'=>array('6'=>array('10'=>array('count'=>76),'11'=>array('count'=>42),'15'=>array('count'=>23),);我只想将多维数组存储在一列中。我的回答似乎是:$data=array('2017'=>array('6'=>array('10'=>array('count'=>76),'11'=>array('count'=>42),'15'=>array('count'=>23),);我只想将多维数组存储在一列中。我似乎回答了?