PHP数组序列化键值CodeIgniter缓存

时间:2013-12-05 13:20:46

标签: php codeigniter

我有表名'首选项'列(键,值)

http://imageshack.com/a/img202/2909/lvaz.jpg

我在codeigniter中使用缓存

看看这个:

$pref = $this->ci->db->get('preferences')->result();

$this->ci->cache->save('preferences', $pref, 30000);

保存缓存:

a:3:{s:4:"time";i:1386246188;s:3:"ttl";i:30000;s:4:"data";a:87:{i:0;O:8:"stdClass":2:{s:3:"key";s:10:"site_title";s:5:"value";s:13:"CARS Big";}i:1;O:8:"stdClass":2:{s:3:"key";s:11:"forum_title";s:5:"value";s:14:"CARS Big forum";}i:2;O:8:"stdClass":2:{s:3:"key";s:14:"forum_per_page";s:5:"value";s:2:"10";} ...

调用缓存使用:

$data = $this->ci->cache->get('preferences');

print_r($data);

输出:

Array( 
            [0] => Array
            (
                [key] => site_title
                [value] => CARS Big
            )

        [1] => Array
            (
                [key] => forum_title
                [value] => CARS Big forum
            )

        [2] => Array
            (
                [key] => forum_per_page
                [value] => 10
            )

        [3] => Array
            (
                [key] => forum_section_per_page
                [value] => 10
            )

        [4] => Array
            (
                [key] => forum_replies_per_page
                [value] => 5
            )

        [5] => Array
            (
                [key] => forum_can_add_pictures
                [value] => 1
            )

        [6] => Array
            (
                [key] => forum_can_add_poll
                [value] => 1
            )

        [7] => Array
            (
                [key] => forum_can_set_time_to_close
                [value] => 1
            )

        [8] => Array
            (
                [key] => forum_can_set_replies_to_close
                [value] => 1
            )

        [9] => Array
            (
                [key] => forum_auto_active_topics
                [value] => 1
            )

        [10] => Array
            (
                [key] => market_title
                [value] => market CARS Big
            )

        [11] => Array
            (
                [key] => market_per_page
                [value] => 5
            )

        [12] => Array
            (
                [key] => market_section_per_page
                [value] => 3
            )


    )

如何使列键的内容成为关键

列值是内容

像这样:

$data['site_title']

我需要打印$ data ['site_title']:CARS Big

以便调用此函数

function pref($key=NULL)
{
$data = $this->ci->cache->get('preferences');
return $data[$key];
}

**

2 个答案:

答案 0 :(得分:1)

基本上你是想循环这些值并重新分配键,所以像这样的东西可以工作:

// loop through data
foreach($data as $k=>$v)
{
    // unset the original array item to get rid of $data[0], $data[1], $data[2] as so forth
    unset($data[$k]);

    // $k is a digit (0,1,2,3,4,5,....)
    // $v is the array of values so $v['key'] is 'site_title' and $v['value'] is 'CARS Big'
    // so essentially we are doing $data['site_title'] = 'CARS Big'; in the line below
    $data[$v['key']] = $v['value'];
}

答案 1 :(得分:-1)

你不能,因为缓存保存实现是一个固定的过程。只有你自己创建缓存。

但您可以像这样执行返回功能

function pref($key=NULL)
{
    // call pref data
    $data = $this->ci->cache->get('preferences');
    if( ! $data ) {
        // cache not present request new
        $data = $this->ci->db->get('preferences')->result();
        $this->ci->cache->save('preferences', $data, 30000);
    }

    // loop
    foreach( $data as $preferences ) {
        if( isset( $preferences['key'] ) && $preferences['key'] == $key ){
            return $preferences['value'];
        }
    }
    return false;
}