array_diff gives Array to String conversion error

2019-08-09 12:40发布

问题:

I am experiencing a weird error from an array_diff statement. The statement is:

$query = array_diff($params, array('f' => array()));

and the var_dump of the $params is array(1) { ["f"]=> array(0) { } }

This happens in a drupal module called Islandora_solr_search and I get the following error message twice like below

Notice: Array to string conversion in IslandoraSolrResults->setBreadcrumbs() (line 427 of /var/www/drupal/sites/all/modules/islandora_solr_search/includes/results.inc).

Notice: Array to string conversion in IslandoraSolrResults->setBreadcrumbs() (line 427 of /var/www/drupal/sites/all/modules/islandora_solr_search/includes/results.inc).

Does anyone know why this happens?

回答1:

array_diff throws notice errors when it finds an array inside an array. See the comment by Michiel Thalen

I may assume that you're running php 5.4 or higher. You can see it by yourself, by checking your array_diff statement in the sandbox (you can switch php versions there)

There's also a discussion in Drupal forums

As a quickfix I suggest this:

$query = @array_diff($params, array('f' => array()));

And in case you're going to use array_diff function with deep arrays, there are plenty of solutions on the net, including official php.net resource.