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

13

42

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)
Emlynne answered 25/1, 2011 at 19:40 Comment(3)
Did you migrate your module or site to drupal 7? if it is a custom module where is the code? Also you should try switching off modules until the warnings go away to narrow it down more. Something is giving the wrong parameters.Tallage
Yes, I have migrated my module to Drupal 7. But the error is in entity.inc file which is a core file of Drupal 7.Emlynne
Regarding the var_dump output above: No, in the key/value pair is not in correct format. The array_flip function can flip an array like array(0 => 'a'), but not array(0 => array(0 => 'a')) because the nested array would have to become the array key.Iodize
L
86

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".

Lunulate answered 26/1, 2011 at 10:46 Comment(4)
Thank you so much. That was the problem. I was doing $node = node_load(array('nid' => $node->nid)); and $thisUser = user_load(array('uid' => $service->uid)) at many places. Also thanks for tip to do quick search using "_load(array". That was brilliant!Emlynne
Thanks. I've found exampels of such use even in the core : form.inc, trigger module. Also community modules "pathauto" & "page_title" still use old syntax.Impress
or if you try to pass null/false values into load functions eg: $nid=null; node_load($null);Counterwork
Just to add, entity_load() requires that the IDs be an array, i.e. entity_load('field_collection_item', array(123));Antitype
K
10

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() );
Koziarz answered 26/1, 2011 at 1:39 Comment(0)
C
6

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

Cammi answered 22/2, 2012 at 18:47 Comment(1)
This was my issue - I managed to track down what was being sent in and it was alot of arrays consisting of array(0 => NULL)Bouleversement
R
3

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.

Rattat answered 28/6, 2011 at 2:13 Comment(0)
O
1

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/

Otterburn answered 26/4, 2011 at 18:1 Comment(0)
I
0

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.

Iodize answered 25/1, 2011 at 20:46 Comment(2)
Probably you are right. I need to better look on issue queue at Drupal.org. I was just hoping that if I could get some suggestions on array_flip() and how not to use it.Emlynne
No. I was not using Insert Module. Just the modules that I mentioned above.Emlynne
T
0

Example of good use:

<?php
$user=user_load(arg(1));
$username=$user->name;
print strtolower(preg_replace('/[^a-zA-Z0-9\-]/si' , '-' , $username));
?>
Theomania answered 14/11, 2012 at 22:50 Comment(0)
L
0

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.

Lema answered 22/11, 2012 at 0:42 Comment(0)
T
0

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'])).

Threecornered answered 22/8, 2018 at 10:15 Comment(0)
P
0

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.

Plated answered 10/12, 2020 at 16:42 Comment(0)
Z
0

You can clean the array from null values before flipping

Like this

$my_array = array_filter($my_array);
$my_array = array_flip($my_array);
Zantos answered 8/6, 2022 at 14:53 Comment(0)
D
-1

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!!

Daytoday answered 27/7, 2012 at 20:47 Comment(0)
S
-6

Here is simple solution :)

Edit settings.php file and add this line

error_reporting(0);
Strength answered 19/6, 2011 at 15:35 Comment(2)
1) Don't hack core. 2) Don't do this. Turn off error reporting on production sites only, after development is done, and do it the right way -- through Drupal's UI.Countryandwestern
What Adam said, or else write your own module to do it in code, and only enable that module on production.Intra

© 2022 - 2024 — McMap. All rights reserved.