1

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.

thordarson
  • 5,943
  • 2
  • 17
  • 36

2 Answers2

1

Access the value using:-

$cookie_value["37"]

"37" (key) is in string format.. You are specifying it as integer ($cookie_value[37])

The Real Coder
  • 138
  • 1
  • 7
  • PHP is [loosely typed](http://en.wikipedia.org/wiki/Weak_typing), so this shouldn't matter, but since I've got no other ideas I tried. I added some more debugging code to my question, all of them return false. – thordarson Jan 30 '13 at 17:17
  • what does the `print_r($cookie_value["37"])` outputs? – The Real Coder Jan 30 '13 at 17:22
  • Any attempt to access the `$cookie_value['37']` results in an `ErrorException [ Notice ]: Undefined index: 37` – thordarson Jan 30 '13 at 17:26
1

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

?>
Tom
  • 843
  • 7
  • 12
  • Thanks for the suggestion. The Real Coder already suggested something similar so I added some more debugging code where I tried to typecast 37 to a string. It didn't work. – thordarson Jan 30 '13 at 17:25
  • I've just seen you've updated your question, so the problem isn't related to the types. It could still be to do with casting to an array - what does var_dump show if you pass `true` as the second parameter to json_decode? – Tom Jan 30 '13 at 17:26
  • Wow, that's it! I took out the typecast and passed true as the second parameter, and it works. Thanks man! I wonder if that's by design or a bug in PHP. – thordarson Jan 30 '13 at 17:32
  • 1
    Nice. I'll update my answer so that the json_decode is read before the type checking. – Tom Jan 30 '13 at 17:42