The Silent Killer in array_flip() on Drupal 7
You are seeing a flood of warnings: array_flip(): Can only flip STRING and INTEGER values!. This error is notoriously silent regarding which value caused the crash, yet it halts execution or corrupts data structures depending on your PHP configuration. In the specific context of moving to Drupal 7 (PHP 5.3.x), this usually points to a mismatch between how entity IDs are stored in cache versus how they are retrieved.
The stack trace consistently points to includes/entity.inc, line 178, inside DrupalDefaultEntityController::load(). The code snippet responsible is typically:
$passed_ids = !empty($ids) ? array_flip($ids) : FALSE;
When you inspect the $ids variable just before this line, as you did with var_dump, you see arrays containing nested structures or unexpected types. In your specific case, the dump shows values like '0' (string) alongside objects or complex arrays where integers are expected. The PHP documentation is clear: array_flip() expects keys and values to be valid string or integer keys. If a value contains an object, a resource, or a malformed string that cannot be cast to a valid key, the function throws a warning and skips that pair.
Why This Happens in Migrations
When migrating a module to Drupal 7, especially one relying on CTools or custom entity logic, the root cause is often how data was serialized into the cache table (cache_data or cache_entity). If an earlier version of your code stored complex objects (like associative arrays representing entity bundles) directly into a field meant for simple IDs, PHP might have implicitly converted them to strings like "0" or "1".
When the new code attempts to load these entities, it pulls these corrupted cache entries. The array_flip operation then fails because the internal representation of the ID list includes non-standard types. The fact that you are on a development environment (xampp) makes this more visible; production servers might suppress these warnings but still return incomplete data, leading to subtle bugs later.
Debugging and Fixing
To resolve this, you must ensure that $ids contains only primitive types before reaching entity.inc. However, fixing the core function is rarely the right move because DrupalDefaultEntityController is part of the API contract. The real fix lies in cleaning your data source or adjusting how you interact with the cache.
If you can modify the module code that populates the cache, ensure you are serializing only primitive types (strings and integers). If you cannot touch the cache directly, check for custom entity loaders or hook implementations that might be injecting malformed data.
A common pattern to safely handle this during debugging is to wrap the operation:
if (!empty($ids)) {
// Check if any element is not a string or integer
$safe_ids = array_filter($ids, function ($value) {
return is_string($value) || is_int($value);
});
if (count($safe_ids) === count($ids)) {
$passed_ids = array_flip($ids);
} else {
// Handle the case where some IDs were invalid
// Log the issue or skip loading for those specific IDs
watchdog('entity', 'Skipping flip due to non-primitive values in cache.',
array('%count' => count(array_diff($ids, $safe_ids)), '%type' => 'string|int'),
WATCHDOG_WARNING);
// Decide whether to return FALSE or handle missing entities gracefully
$passed_ids = FALSE;
}
} else {
$passed_ids = FALSE;
}
This approach prevents the warning from crashing your request and allows you to log exactly which entries are corrupt.
The Laravel Context
While this issue is specific to Drupal 7's legacy architecture, it highlights a universal principle in backend development: never trust data coming from serialized caches or external sources without validating types first. Modern frameworks like Laravel handle this differently; they use type-safe collections and strict serialization formats (JSON) that inherently prevent storing complex objects as simple keys. If you are refactoring logic to be compatible with newer ecosystems, consider how your data structures will translate when moving away from PHP's loose typing rules.
In summary, the error is not a bug in array_flip, but a symptom of data corruption in your entity cache. Clean up the source data ensuring only valid IDs reach the controller, and wrap your logic to handle edge cases gracefully rather than relying on warning suppression.