Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9211419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:17:28+00:00 2026-06-18T01:17:28+00:00

This is a weird one, so bear with me. Asking you guys really is

  • 0

This is a weird one, so bear with me. Asking you guys really is my last resort.

To be clear, I’m using Kohana 3.3, although I’m not even sure that’s related to the problem.

I’m using a cookie to track read items on a website. The cookie contains a json_encoded array. Adding to that array isn’t a problem, objects are added every time a new item is read. An item in the array contains an object with the last_view_date and a view_count For the sake of updating the view count, I need to check whether the item has been read already using array_key_exists and then add to the view count. Here’s how I’m setting the cookie:

// Get the array from the cookie.
$cookie_value = Cookie::get_array('read_items');

// Update the view_count based on whether the id exists as a key.
$view_count = (array_key_exists($item->id, $cookie_value)) ? 
                $cookie_value[$item->id]['view_count'] + 1 : 1;

// Create the item to be added to the cookie.
$cookie_item = array(
    'last_view_date' => time(),
    'view_count' => $view_count
);

// Push $cookie_item to the cookie array.
Cookie::push('read_items', $item->id, $cookie_item);

I’ve added two methods to Kohana’s Cookie class, Cookie::push and Cookie::get_array, used in the code above:

class Cookie extends Kohana_Cookie {

    public static function push($cookie_name, $key, $value)
    {
        $cookie_value = parent::get($cookie_name);

        // Add an empty array to the cookie if it doesn't exist.
        if(!$cookie_value)
        {
            parent::set($cookie_name, json_encode(array()));
        }
        else
        {
            $cookie_value = (array)json_decode($cookie_value);

            // If $value isn't set, append without key.
            if(isset($value))
            {
                $cookie_value[$key] = $value;
            }
            else
            {
                $cookie_value[] = $key;
            }
            Cookie::set($cookie_name, json_encode($cookie_value));
        }
    }

    public static function get_array($cookie_name)
    {
        return (array)json_decode(parent::get($cookie_name));
    }
}

Now, here’s my problem. Running a var_dump on $cookie_value outputs the following:

array(1) {
  ["37"]=>
  object(stdClass)#43 (2) {
    ["last_view_date"]=>
    int(1359563215)
    ["view_count"]=>
    int(1)
  }
}

But when I try to access $cookie_value[37], I can’t:

var_dump(array_key_exists(37, $cookie_value));
// Outputs bool(false);

var_dump(is_array($cookie_value));
// Outputs bool(true);

var_dump(count($cookie_value));
// Outputs int(1);

var_dump(array_keys($cookie_value));
// Outputs:
// array(1) {
//   [0]=>
//   string(2) "37"
// }

Added debugging code:

var_dump(isset($cookie_value["37"]));
// Outputs bool(false).

var_dump(isset($cookie_value[37]));
// Outputs bool(false).

var_dump(isset($cookie_value[(string)37]));
// Outputs bool(false).

I hope it’s clear enough.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:17:29+00:00Added an answer on June 18, 2026 at 1:17 am

    Take a look at json_decode. Currently, you’re casting the result to an array. If you pass true as the second parameter to json_decode, you’ll get an array back instead of a stdObject.

    The problem may also be related to you checking int vs string keys. You could test by running this code on your server:

    <?php  
    
    $one = array("37" => "test");
    $two = array(37 => "test");
    
    var_dump(array_key_exists(37,$one)); // true or false?
    var_dump(array_key_exists(37,$two)); // true
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I dunno guys, this is a really weird one, but I might just be
This is a weird one and I don't really even know what to search
Ok, this is a really weird one. I have an MPI program, where each
Boy, this one is really weird. I expect the following code to print 1990,
Ok, this one is really weird... I can't show code for it exactly cause
This is a bit of a weird one. I'm using HTTPClient 4.1.2, and it
This is a weird one. Long story short: wrote a usercontrol using AJAX. Used
This is a weird one. I am using the ExecWB method to create a
This is a tricky one to explain (and very weird), so bear with me.
this is a weird one. i have a -pre-compile target in my custom_rules.xml .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.