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']))
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 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.
I'm working on a PHP script that updates some tracking numbers based on an uploaded CSV file. The import worked fine for some time, then the exports started having quotation marks around the values. I thought this would be fine, but it started rejecting the files. Doing some debugging and var_dumps, I discovered a very strange situation I have never seen before - An associative array with two indices with the same name. I ran the code setting the fields (shown below) and added a line:
$v['order_id'] = '119205';
After running that line, the var_dump was as follows:
array(15) {
["order_id"]=>
string(6) "119205"
["Tracking Number"]=>
string(22) "6735675476254654756"
["Postage"]=>
string(4) "1.64"
["order_id"]=>
string(6) "119205"
}
Some fields removed for brevity. As you can see, there are two ["order_id"] indices. How is this even possible?
Here is the code that sets the values of the array dumped above:
$v = array();
foreach ($map as $k => $n) {
$v[$n] = #$data[$k];
}
with $map being the CSV header row. Trying to reference $v['order_id'] without running the $v['order_id'] = '119205'; line resulted in this error:
Notice: Undefined index: order_id in /dir/to/php/file/php_file.php</b> on line 29
Manually setting the index worked as expected, pulling the rest of the data from $v without issue.
EDIT:
Dumping the array_keys resulted in:
[0]=>
string(11) "order_id"
and:
[14]=>
string(8) "order_id"
making the first one three characters longer.
var_export still resulted in identical indices.
How can I get rid of these invisible characters? I've already tried $v[trim($n)] = #$data[$k]; in the foreach().
Try var_dump(array_keys($v)). Find the key that looks like order_id and make sure the string's length is exactly 8. I suspect there may be a NUL character in there, which would give it a length of 9 and cause it to not respond to order_id.
Quote:
In the output of var_dump(), the null bytes are not visible.
Technically you can not have two times the same key in PHP in an array. It might be that var_dump is not giving the right keys here (e.g. probably some null-chars or other non-displayable chars are dropped).
Instead you might want to check, what's going on:
var_dump(array_keys($data));
Maybe it helps, the following is a related question which demonstrates when var_dump hides some information:
Array to Object and Object to Array in PHP - interesting behaviour
On stream.get, I try to
echo $feeds["posts"][$i]["attachment"]["href"];
It return the URL, but, in the same array scope where "type" is located (which returns string: video, etc), trying $feeds["posts"][$i]["attachment"]["type"] returns nothing at all!
Here's an array through PHP's var_dump: http://pastie.org/930475
So, from testing I suppose this is protected by Facebook? Does that makes sense at all?
Here it's full: http://pastie.org/930490, but not all attachment/media/types has values.
It's also strange, because I can't access through [attachment][media][href] or [attachment][media][type], and if I try [attachment][media][0][type] or href, it gives me a string offset error.
["attachment"]=> array(8) {
["media"]=> array(1) {
[0]=> array(5) {
["href"]=> string(55) "http://www.facebook.com/video/video.php?v=1392999461587"
["alt"]=> string(13) "IN THE STUDIO"
["type"]=> string(5) "video"
My question is, is this protected by Facebook? Or we can actually access this array position?
Well, once the data is returned to you, it can no longer be protected by Facebook. You have full access to everything in that result as a regular data structure.
From the looks of it, there are multiple href properties throughout, so you'll want to be careful which one you're going for. $feeds["posts"][$i]["attachment"]["href"] is a valid element for some items, but $feeds["posts"][$i]["attachment"]["media"][0]["href"] is also a valid element.
There doesn't appear to be a $feeds["posts"][$i]["attachment"]["type"] element though, so that's why you're getting nothing for that particular item. There is a type inside ["attachment"]["media"][0] however, which is probably what you want.
If you are getting a string offset error when using array syntax, you've probably mixed up an element somewhere. Strings can be accessed via array syntax. For example:
$str = "string";
echo $str[1]; //echos 't'
You would get an offset warning if you tried to access an index that was larger than the string. In any case, from the looks of that output, $feeds["posts"][$i]["attachment"]["media"][0]["type"] should work.
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)