Ajinkya Kulkarni
Ajinkya Kulkarni

Reputation: 1022

array_flip():Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load()

I have recently migrated my module to Drupal7 (on PHP Version 5.3.1) and now I am getting following errors:

    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 354 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 354 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 354 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).

I have also tried upgrading other modules and core to latest versions as mentioned here http://drupal.org/node/1022736

entity 7.x-1.x-dev (2011-Jan-24), views 7.x-3.x-dev (2011-Jan-22), Drupal core 7.x-dev (2011-Jan-24), profile2 7.x-1.0-beta1, references 7.x-2.x-dev (2011-Jan-14), ctools 7.x-1.0-alpha2

I am not able to figure out what is exactly causing this error?

Edit:

According to http://php.net/manual/en/function.array-flip.php,

array_flip() returns an array in flip order, i.e. keys from trans become values and values from trans become keys.

Note that the values of trans need to be valid keys, i.e. they need to be either integer or string. A warning will be emitted if a value has the wrong type, and the key/value pair in question will not be flipped.

I have done the var_dump($ids); before line 178 in entity.inc ( $passed_ids = !empty($ids) ? array_flip($ids) : FALSE;)

And it looks to me that key/value pair is always in correct format(?).

array
  0 => 
    array
      'nid' => string '6' (length=1)

array
  0 => 
    array
      'uid' => string '1' (length=1)

array
  0 => string '0' (length=1)

array
  0 => 
    array
      'nid' => string '7' (length=1)

array
  0 => 
    array
      'nid' => string '4' (length=1)

array
  0 => 
    array
      'nid' => string '8' (length=1)

Upvotes: 44

Views: 76604

Answers (13)

boksiora
boksiora

Reputation: 1198

You can clean the array from null values before flipping

Like this

$my_array = array_filter($my_array);
$my_array = array_flip($my_array);

Upvotes: 0

simhumileco
simhumileco

Reputation: 34623

When such errors occur on the Drupal, my suggestion is to check firstly the "Recent log messages" list in the Admin panel:

enter image description here

This page has been very helpful to me many times.

Tip: Sometimes it makes sense to look at not only the last Error but also the Warning and Notice messages that immediately precede it. Details of one of them usually contain a significant hint to solve the problem.

Upvotes: 0

Enno
Enno

Reputation: 1862

I've also gotten this message when erroneously trying to load multiple nodes via node_load_multiple($nids) where $nids was not an array of node-ids.

For example, using the result of an EntityFieldQuery, and calling node_load_multiple($result['node']) instead of node_load_multiple(array_keys($result['node'])).

Upvotes: 0

Raghibsuleman
Raghibsuleman

Reputation: 5

Here is simple solution :)

Edit settings.php file and add this line

error_reporting(0);

Upvotes: -6

kenorb
kenorb

Reputation: 166735

This could be problem of the bad coding (e.g. loading invalid entities or running some old Drupal 6 code in Drupal 7):

  • If you've any custom modules, double check your code for common mistake
  • If you're using contrib module, find the right bug against it and apply the patch (if available) or raise the new one.

Troubleshooting:

  • You may try to dump the backtrace by calling and printing print_r(debug_backtrace()),
  • Use your scm tool to get back to the point where it was working fine, and compare the changes, so eventually you'll find where the problem is (e.g. gitk, git log --patch, etc.)
  • please use Coder Review module to find bugs in your code after the update (e.g. drush --contrib --no-empty --upgrade7x coder-review).
  • Install XDebug to track your issue by trace log or step-by-step debugging.

Alternatively you may debug your code by defining the following temporary hook:

/**
 * Implements hook_watchdog().
 */
function foo_watchdog($log_entry) {
  if ($log_entry['type'] == 'php' && $log_entry['severity'] <= WATCHDOG_WARNING) {
    // Old school
    var_dump(debug_backtrace()); // Optionally add: exit();

    // Devel: Log the backtrace into temporary file: drupal_debug.txt
    // Locate via: $ drush eval "echo file_directory_temp() . '/drupal_debug.txt'"
    function_exists('dd') && dd(debug_backtrace());
  }
}

Clear the cache before testing it.

It'll print the backtrace on every PHP warning or error with arguments, so you can find your invalid parameters which were passed into the Drupal core.

Upvotes: 1

warmth
warmth

Reputation: 467

Example of good use:

<?php
$user=user_load(arg(1));
$username=$user->name;
print strtolower(preg_replace('/[^a-zA-Z0-9\-]/si' , '-' , $username));
?>

Upvotes: 0

Shilpa
Shilpa

Reputation: 309

Thanks for the post it worked for me!, I was encountering with this problem in Drupal 7 since long time and could fighure out the problem. Bottom line

"Do not pass array values to array_flip for any kind of entities, eg: If you are trying to load and entity user_load() or field_collection_item_load() for loading field collection items, Pass only values in the string rather than array itself."

Thanks!!

Upvotes: -1

Johnathan Elmore
Johnathan Elmore

Reputation: 2244

The problem comes up when you're using Organic groups field access (Organic Groups 7.x-1.3)

You can usually disable that sub-module unless you do field level access control with OG.

http://drupal.org/node/1102570#comment-5626946

Upvotes: 6

Evan Donovan
Evan Donovan

Reputation: 766

This can also happen when you call entity_load with an array that is not an array of entity id's as the second argument - see http://api.drupal.org/api/drupal/includes--common.inc/function/entity_load/7 and http://drupal.org/node/1160566 to understand why.

Upvotes: 3

Jeremy
Jeremy

Reputation: 11

Saw a similar issue in our usage of the latest page_title module. For now, we just disabled the module and it cleaned up the error.

See: http://www.newblood.com/blog/2011/04/26/drupal-7-error-in-page-title-module/

Upvotes: 1

Berdir
Berdir

Reputation: 6891

The most common cause of this error is using a something_load() function with an array as argument. This is not supported anymore because the load_multiple() functions need to be used for this now.

Example in D6:

<?php
// Using array with the id was already discouraged in D6 but still worked.
$user = user_load(array('uid' => 1));
$user = user_load(array('name' => 'admin'));
?>

Drupal 7:

<?php
// Argument to a load() function *must* be a single id
$user = user_load(1);

// Querying for another attribute is a bit more complex.
// Note that using reset(user_load_multiple() directly is not E_STRICT compatible.
$users = user_load_multiple(array(), array('name' => 'admin'));
$user = reset($users);
?>

So, the easiest way to catch these is to search for "_load(array".

Upvotes: 86

Matt V.
Matt V.

Reputation: 9809

I ran into the same array_flip error over the weekend, trying to upgrade a custom module to Drupal 7. The problem is that a nested array is getting passed into DrupalDefaultEntityController, but it's expecting a simple array of integers or strings. In my case, I was passing in a nested array in to EntityFieldQuery, when it wants just an array of integers.

To better track down the code that is calling DrupalDefaultEntityController, try inserting the following before line 178 in entity.inc:

drupal_set_message(var_export(debug_backtrace(), TRUE));

... or preferably, install the Devel module and try inserting the following instead:

dpm( debug_backtrace() );

Upvotes: 10

marcvangend
marcvangend

Reputation: 5642

Are you using Insert Module? See http://drupal.org/node/850946.

When it comes to specific errors like this, I think you're better off searching the issue queue at drupal.org than asking on SO.

Upvotes: 0

Related Questions