I have this bit of code:
if( array_key_exists('customtextcolor',$atts) ){
// If in array, then check if none, if not none, add to CSS classes
if ( 'none' !== $atts['customtextcolor'] ) {
$custstyles[] = $atts['customtextcolor'];
}
}
Which is on line 90, and my php.log shows an error on line 90.
Apr 2 08:46:16 #####.net ool www: PHP Warning: array_key_exists() expects parameter 2 to be array, string given in /var/www/####/wp-content/plugins/Fusion-Builder-Custom-Text-For-HP/fusion-builder-custom-text-for-hp.php on line 90
Which I thought was strange, because I was sure it is was an array, because the code works as expected. I also used echo gettype($atts); and it returns an array. When I var_dump($atts); it renders the full array.
Any idea what php.log is showing this error message?
If $atts is a string, why does gettype() identify it as an array? And why does var_dump() render it as an array?
I've looked at the other array_key_exists() questions on StackOverflow, but they aren't the same issue as far as I can tell.
Change the if condition as below:
if(is_array($atts) && array_key_exists('customtextcolor', $atts))
This will check if $atts is an array at first then will check the array_key_exist operation.
Just write the following line of code:
array_key_exists('customtextcolor', (array) $atts);
This will cast the $atts-variable into an array if it's not already an array.
You can also just check if there is an array with a value per:
isset($atts['customtextcolor']);
Related
I'm new in php and having problem with a piece of my code if the key given exist. The code of error are below and i don't have a idea what is causing it. Please help.
"Warning: array_key_exists() expects parameter 2 to be array, boolean given in"
The line
if ( is_array(array_key_exists('sizes',wp_get_attachment_metadata($attach_id)) == false ){}
As ArSeN already pointed out, is_array(array_key_exists( doesn't make much sense. I guess you were trying: a) is not array or b) lacks a specific key.
Using a temporary variable you can do something like
if ( !is_array($meta=wp_get_attachment_metadata($attach_id)) || !array_key_exists('sizes', $meta) ) {
// ....
}
Or, in case you try to access the meta data more often (or simply as a coding style):
$meta=wp_get_attachment_metadata($attach_id);
if ( !is_array($meta) || !array_key_exists('sizes', $meta) ) {
// ....
}
You could drop a ternary into that thing to uglify the hell out of it.
if ( is_array(array_key_exists('sizes',wp_get_attachment_metadata($attach_id)) == false) ?: array() ){}
This way if the attachment metadata does not exist, it will provide an empty array, which in turn will produce a false from array_key_exists, which is similar to the behave of the desired code.
When i submit my form and if the second parameter is empty.
It throws error.
Message: in_array() expects parameter 2 to be array, null given
Filename: user/Users_groups.php Line Number: 250
I know there is no second parameter. $this->session->userdata('modify')
if (!in_array('user/users_groups', $this->session->userdata('modify'))) {
$this->error['warning'] = 'You do not have permission to modify';
}
Is there away to make it stop throwing that error when user or I try to submit form if no second parameter $this->session->userdata('modify')
But still have that code.
Try to check first if parameters are valid:
<?php
if(is_array($this->session->userdata('modify')) && !empty($this->session->userdata('modify')))
{
if (!in_array('user/users_groups', $this->session->userdata('modify')))
{
$this->error['warning'] = 'You do not have permission to modify';
}
}else{/* handle errors */}
?>
Replace the null value with an empty array before the if statement:
Could use a ternary operator like this:
$array = (is_array($this->session->userdata('modify')))
? $this->session->userdata('modify')
: Array()
It will use the 'modify' array if it's an array, else will pass an empty array
I would also check array_search() function because it can be compared with NULL wich is returned if irregular parameters set.
5.3.0 As with all internal PHP functions as of 5.3.0, array_search() returns NULL if invalid parameters are passed to it.
I face a problem like this:
$area="Dhaka";
isset($area); //returns true which is OK
isset($area['division']); //returns true why?
// actually, any array key of area returns true
isset($area['ANY_KEY']);//this is my question 1
isset($area['division']['zilla');//now it returns false.
//as I know it should returns false but why previous one was true.
Now if I do this:
$area['division'] = "Dhaka";
isset($area); // returns true which is OK
isset($area['division']); // returns true it's also OK
isset($area['ANY_KEY']); // returns false. I also expect this
isset($area['division']['ANY_KEY']); // returns true why? question #2
Basically both of my questions are the same.
Can anyone explain this?
As with every programming language in existence, a string is stored as an array of characters.
If I did:
$area = "Dhaka";
echo $area[0];
It would return D.
I could also echo the whole string by doing:
echo $area[0].$area[1].$area[2].$area[3].$area[4];
PHP will also type juggle a string into 0 when passed in a manner that accepts only integers.
So by doing:
echo $area['division'];
You would essentially be doing:
echo $area[0];
and again, getting D.
That's why isset($area['division']) returns a true value.
Why doesn't $area['foo']['bar'] (aka $area[0][0]) work? Because $area is only a single-dimension array.
The best approach to handle this problem when you're working with a variable that could either be a string or an array is to test with is_array() before trying to treat your variable as an array:
is_array($area) && isset($area['division'])
PHP lets you treat a string as an array:
$foo = 'bar';
echo $foo[1]; // outputs 'a'
So
$area['division']
will be parsed/executed as
$area[0];
(the keys cannot be strings, since it's not REALLY an array, so PHP type-converts your division string by its convert-to-int rules, and gives 0), and evaluate to the letter D in Dhaka, which is obviously set.
Okay, here's a solution rather than explaining why isset isn't going to work properly.
You want to check if an array element is set based on it's index string. Here's how I might do it:
function isset_by_strkey($KeyStr,$Ar)
{
if(array_key_exists($KeyStr,$Ar))
{
if(strlen($Ar[$KeyStr]) > 0 || is_numeric($Ar[$KeyStr] !== FALSE)
{
return TRUE;
}
return FALSE;
}
}
isset_by_strkey('ANY_KEY',$area); // will return false if ANY_KEY is not set in $area array and true if it is.
The best way to access a linear array in php is
// string treated as an linear array
$string= "roni" ;
echo $string{0} . $string{1} . $string{2} . $string{3};
// output = roni
It is expected behaviour.
PHP Documentation covers this
You can try empty() instead.
If it is returning true for keys that do not exist there's nothing you can do; however, you can make sure that it doesn't have a negative effect on your code. Just use array_key_exists() and then perform isset() on the array element.
Edit: In fact, using array_key_exists() you shouldn't even need isset if it is misbehaving just use something like strlen() or check the value type if array_key_exists returns true.
The point is, rather than just saying isset($Ar['something']) do:
if(array_key_exists('something',$Ar) )
and if necessary check the value length or type. If you need to check the array exists before that of course use isset() or is_array() on just the array itself.
In my code I have a statement like this:
$last_100_char = substr($str[0], -100);
I want to retrieve the last 100 characters of the first element of an array, but while doing this I,m getting some error
substr() expects parameter 1 to be string, array given.
how to fix this?
The error has occurred because the first parameter ie, $str[0] is still an array. As another more efficient method, to obtain the first element of an array you can use array_shift() method.
$first=array_shift(array_values($array));
echo substr($first, -100);
Your example is working fine when I test it.
Make sure that your $str[0] actually contains a string by doing print_r( $str );
I got the answer
Initially I did
$str[] = explode(';',$job_record ['JD']);
$last_100_char = substr($str[0], -100);
So I'm getting that error..
Now I made it as
$str['data']=explode(';',$job_record ['JD']);
$last_100_char = substr($str['data'][0], -100);
Now I'm getting the correct output...
I am receiving the following warning:
Warning: reset() expects parameter 1 to be array, null given in /data/9/1/115/118/1767118/user/1910351/htdocs/aw/home/wp-content/themes/awthemesnew/library/sidebars.php on line 183
Keep in mind I am editing a wordpress .php file. Here is the full code on line 183:
function theme_print_sidebar($name, $places) {
$style = theme_get_option('theme_sidebars_style_' . $name);
$place_count = count($places);
if ($name != 'footer' && $place_count < 2) {
theme_print_widgets(reset($places), $style);
return;
}
?>
Help. Thanks!
The value of $places is apparently null by the time you call reset.
Your code is saying "only call reset when the value of $place_count is less than 2".
You're setting the value of $place_count via the statement:
$place_count = count($places);
We can infer that when count() is called on a null variable it returns 0. Since 0 is less than 2, the following statement is executed:
theme_print_widgets(reset($places), $style);
However, at this point the fact that $places is null is causing an error. I would be curious to know under what circumstances $places is null. Once you've got the answer to that you can decide how to handle that case.
I figured it out. I had erased a command in the footer and when the code when looking for it, since it was gone, it gave me a array warning. Thanks for the help.