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?
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.
Related
Looked at many similiar problems and the solutions didn't help me. I'm getting a strange error message Warning: Illegal string offset 'officeName' in... and the var_dump of the variable generating the error looks like this:
array(10) {
["officeId"]=>
string(5) "11237"
["officeName"]=>
string(37) "Pro Office Inc."
}
The code that produces the errors is:
foreach($objects as $key => $value){
var_dump($value);
}
So $value is an array. What's wrong with what I'm doing and how can I fix it?
Make a double check of every value from the array, since one of these items may be an array, but there may be others which are not. In such cases it is always better to var_dump() the whole $objects array instead of each item, since it's easier to spot any error.
Besides, the Illegal string offset error usually hints that you're trying to treat a string as an array and/or access it's keys, which don't exist.
I am trying to sort a multidimensional array by multiple values but I keep getting the error:
Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in -- on line 19
Line 19 is where I call the array_multisort function:
array_multisort($column1, $column2, $column3, $row);
I have been unable to replicate the issue in a test, so I haven't had much luck in solving the issue.
So basically I am saving a MySql table as a multidimensional associative array. Then I am trying to sort the rows by three different columns. I have checked and all of the arrays passed into the array_multisort() function are the same size. I checked both by manually looking through every row and by using sizeof().
Any ideas what could be causing this and/or what the solution is?
to answer your original question, this normally happens with an uninitiated variable.
For example:
foreach ($arr_this_referers as $int_key => $arr_row) {
$arr_vol[$int_key] = $arr_row['int_cnt'];
}
will cause WARNING due to uninitiated variable $arr_vol but initialize variable before the for loop:
$arr_vol = array();
foreach ($arr_this_referers as $int_key => $arr_row) {
$arr_vol[$int_key] = $arr_row['int_cnt'];
}
array_multisort($arr_vol, SORT_DESC, $arr_this_referers);
... & warning disappears, hope this helps.
I ended just using uasort() instead. it gave me no problems at all and it was pretty straight forward to set up the cmp logic.
Still no idea what the issue with array_multisort() was.
As per documentation, I've tried below code to find size of array in codeigniter
echo element('size', $get, NULL);
but it ended up showing following error
( ! ) Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\cinifb_ci\system\helpers\array_helper.php on line 46
I've tried to load content of $get into another array variable, but it continued showing the above error.
Please suggest me the alternative ways along with solving this.
I've tried to using native PHP solutions like
echo size_array($get);
but It ended up in
Fatal error: Call to undefined function size_array()
Does this mean I'm not supposed to use native PHP functions in CodeIgniter
count($array);
That's the native function to get the size of an array ;)
Within CodeIgniter, this expression:
element('size', $get, null)
Only works if $get is an array and it has an index 'size'; if it is an array, it would be more likely that you meant this:
count($get);
However, in your case, $get is actually an object, stdClass to be exact; determining the size of that object requires another step:
count(get_object_vars($get));
Example:
$completed_requests = $this->db->where('status' => SOLICITACAO_FINALIZADA)
->where('trip_occurred' => OCORREU)
->count_all_results('transport_requests');
->count_all_results('TABLE_NAME');
AFAIK - in_array() should return TRUE or FALSE.
In my case, It does validate as true - but still throwing an error:
[function.in-array]: Wrong datatype for second argument
The line is this :
in_array($key,$instance['cfl2']);
and the $instance['cfl2'] is a verified array which looks like this :
array(2) { [0]=> string(8) "price" [1]=> string(6) "age" }
My questions are :
What am I doing wrong.
Why it is throwing an error (but still working fine and returns true)
Is the problem occur because I use some kind of nested array ? (meaning that an array item $instance['cfl2'] is actually an array by itself ?
I also tried $is = $instance['cfl2'] and in_array($key,$is) - but the result was the same error.
You can cast a variable to an array to avoid this error:
in_array($key, (array) $instance['cfl2'])
in_array() will deal as in_array("search", $instance).
If you are using a nested or multidiamentional array, then in_array() wont work and you should write a separate function to handle this. Or use array_key_exists() instead. It will work for certain specific situations. Find out if your requirement is met.
ie
if(array_key_exists($key,$instance['cfl2']))
I ran into this bug where an element of an array, if its index is the string "0", is inaccessible.
It's not a bug with unserialize, either, as this occurred in my code without invoking it.
$arr = unserialize('a:1:{s:1:"0";i:5;}');
var_dump($arr["0"]); //should be 5, but is NULL
var_dump($arr[0]); //maybe this would work? no. NULL
Am I doing something wrong here? How do I access this element of the array?
Yes, it looks as though it is a bug, related to PHPs automatic conversion of strings to integers. More information is available here: http://bugs.php.net/bug.php?id=43614
var_dump( $arr ); // => array(1) { ["0"]=> int(5) }
$arr2["0"]=5;
var_dump($arr2); // => array(1) { [0]=> int(5) }
print serialize($arr2); // a:1:{i:0;i:5;}
So it seems that older versions of PHP5 don't perform the string index to integer index conversion in unserialize.
This bug was reported in PHP 5.2.5, and is fixed in PHP 5.2.6 (see http://www.php.net/ChangeLog-5.php#5.2.6).
use var_dump on the structure to see how it's represented . maybe that will help. I was doing the same thing in Perl when I had problems like this with Data::Dumper
Actually, the code in your question yields
int(5)