简体   繁体   中英

PHP array serialize key value CodeIgniter cache

I have table name 'preferences' column(key,value)

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

I use cache in codeigniter

look this :

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

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

save cache :

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

Call cache use:

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

print_r($data);

output:

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
            )


    )

How do I make the content of the column key is the key

And the column value is the content

Like this:

$data['site_title']

I need $data['site_title'] to print : CARS Big

so as to call this function

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

**

Essentially you are looking to loop the values and re-assign the keys so something like this could work:

// 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'];
}

you can't, since the cache save implementation is a fixed process. only if you make your own cache implementation.

but you can perform your return function like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM