I found a strange issue when storing then retrieving an associative array via APCu. I have a feeling it has to do with the internal serialize/deserialize routines, so I don’t know if this is a known or expected behavior. Key-matching on the fetched array is not working:
- PHP 8.2.25 on an x86_64 running RH Enterprise Linux 9.5
- APCu version 5.1.23 with PHP serializer
// build superset array
$arOne = [
"cat" => ["meow", "cat is hungry"],
"dog" => ["woof", "dog is concerned"],
"bird" => ["chirp", "bird is happy"],
];
// specify subset keys to look for
$arTwo = array_flip( ["dog", "bird"] );
// get a subset array,
// by finding the keys that match in the superset array
$arDirect = array_intersect_key( $arOne, $arTwo );
// store our superset array into APCU
apcu_store("animals",$arOne,60000);
// get a copy of our superset array, from APCU
$arOneFetch = apcu_fetch("animals");
// get a second subset array,
// by finding the keys that match in the copy of our superset array
$arFetch = array_intersect_key( $arOneFetch, $arTwo );
Results:
count($arOne) is: 3 / JSON Text: {"cat":["meow","cat is hungry"],"dog":["woof","dog is concerned"],"bird":["chirp","bird is happy"]}
count($arOneFetch) is: 3 / JSON Text: {"cat":["meow","cat is hungry"],"dog":["woof","dog is concerned"],"bird":["chirp","bird is happy"]}
count($arDirect) is: 2 / JSON Text: {"dog":["woof","dog is concerned"],"bird":["chirp","bird is happy"]}
count($arFetch) is: 0 / JSON Text: [] // <-- I expected this result to be the same as $arDirect
var_dump($arOne):
array (size=3)
'cat' =>
array (size=2)
0 => string 'meow' (length=4)
1 => string 'cat is hungry' (length=13)
'dog' =>
array (size=2)
0 => string 'woof' (length=4)
1 => string 'dog is concerned' (length=16)
'bird' =>
array (size=2)
0 => string 'chirp' (length=5)
1 => string 'bird is happy' (length=13)
var_dump($arOneFetch):
array (size=3)
'cat' =>
array (size=2)
0 => string 'meow' (length=4)
1 => string 'cat is hungry' (length=13)
'dog' =>
array (size=2)
0 => string 'woof' (length=4)
1 => string 'dog is concerned' (length=16)
'bird' =>
array (size=2)
0 => string 'chirp' (length=5)
1 => string 'bird is happy' (length=13)
My workaround was to store the array as JSON encoded, then decode after fetch…but I would rather understand why this is happening.