$imageTypes = array("image/gif", "image/jpeg", "image/png","image/jpg");
$uploadData = array_shift($check);
print_r($uploadData)
Array
(
[name] => Chrysanthemum.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\phpAADE.tmp
[error] => 0
[size] => 879394
)
if(!in_array($uploadData['type'],$imageTypes)){
return false;
}
But i m getting error Uninitialized string offset: 0 in this below line
if(!in_array($uploadData['type'],$imageTypes)){
On your piece of code $check is undefined.
$imageTypes = array("image/gif", "image/jpeg", "image/png","image/jpg");
$uploadData = array_shift($check);
if(!in_array($uploadData['type'],$imageTypes)){
return false;
}
Maybe you have to fix by:
$imageTypes = array("image/gif", "image/jpeg", "image/png","image/jpg");
$uploadData = array_shift($imageTypes);
if(!in_array($uploadData['type'],$imageTypes)){
return false;
}
I think I understand what might be happening here. The value you array_shift() from the $check array might be an empty string. Consider the following example:
$imageTypes = array("image/gif", "image/jpeg", "image/png","image/jpg");
$check = array(
'' // empty string
);
$uploadData = array_shift($check);
var_dump(in_array($uploadData['type'], $imageTypes));
This raises the following warnings/notices:
PHP Warning: Illegal string offset 'type' in /Users/darragh/Sites/so.php on line 26
PHP Stack trace:
PHP 1. {main}() /Users/darragh/Sites/so.php:0
PHP Notice: Uninitialized string offset: 0 in /Users/darragh/Sites/so.php on line 26
PHP Stack trace:
PHP 1. {main}() /Users/darragh/Sites/so.php:0
Warning: Illegal string offset 'type' in /Users/darragh/Sites/so.php on line 26
Call Stack:
0.0002 227040 1. {main}() /Users/darragh/Sites/so.php:0
Notice: Uninitialized string offset: 0 in /Users/darragh/Sites/so.php on line 26
Call Stack:
0.0002 227040 1. {main}() /Users/darragh/Sites/so.php:0
bool(false)
Example: https://eval.in/134971
Note that your notice PHP Notice: Uninitialized string offset: 0 is in there, amongst other things.
Either way, you should make your code robust, perhaps raise an exception or deal with the error in your preferred manner. Something like:
$uploadData = array_shift($check);
// die if the data is not an array or if it does not have the expected key
if (!is_array($uploadData) || !array_key_exists('type', $uploadData)) {
exit('$uploadData is not a valid array!');
}
// etc.
return !in_array($uploadData['type'], $imageTypes);
Example: https://eval.in/134972
Hope this helps.
Related
i created custom plugin few years ago and now i have in my admin plugin page error Warning: Undefined array key "home_team" ....
Here is my code:
function DBP_insert_data()
{
global $wpdb;
$table_name = $wpdb->prefix . 'funstar_league';
$DBP_home = $_POST['home_team'];
$DBP_guest = $_POST['guest_team'];
$DBP_home_team_url_logo = $_POST['home_team_url_logo'];
$DBP_guest_team_url_logo = $_POST['guest_team_url_logo'];
$DBP_home_team_url_logo = $_POST['home_team_url_logo'];
$DBP_url = $_POST['url'];
$DBP_place = $_POST['place'];
$DBP_time = $_POST['game_time'];
$DBP_date = $_POST['game_date'];
if (isset($_POST['save'])) {
$wpdb->insert(
$table_name,
array(
'home_team' => $DBP_home,
'guest_team' => $DBP_guest,
'home_team_url_logo' => $DBP_home_team_url_logo,
'guest_team_url_logo' => $DBP_guest_team_url_logo,
'url' => $DBP_url,
'place' => $DBP_place,
'game_time' => $DBP_time,
'game_date' => $DBP_date,
),
array(
'%s', // Use string format
'%s',
'%s',
'%s',
'%s',
)
);
}
}
Here is error post:
Warning: Undefined array key "home_team" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 133
Warning: Undefined array key "guest_team" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 134
Warning: Undefined array key "home_team_url_logo" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 135
Warning: Undefined array key "guest_team_url_logo" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 136
Warning: Undefined array key "home_team_url_logo" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 137
Warning: Undefined array key "url" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 138
Warning: Undefined array key "place" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 139
Warning: Undefined array key "game_time" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 140
Warning: Undefined array key "game_date" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 141
Writing:
define ("MYARR", array(
'TITLE' => "MY TITLE",
) );
And giving:
print_r(MYARR);
No error is returned. It is all ok! I display:
Array
(
[TITLE] => MY TITLE
)
But if i write:
define ("MYARR['TITLE']", "MY TITLE");
I don't get a error, but giving:
print_r(MYARR);
I get:
Warning: Use of undefined constant MYARR - assumed 'MYARR'
And giving:
echo MYARR['TITLE'];
I get this two warning:
1) Warning: Use of undefined constant MYARR
2) Warning: Illegal string offset 'TITLE'
About first warning, it not is correct, becouse as is possible to see above it is declared!
I think which is a bug of PHP. From a side is allowed declare constant of array but of other side not allowed. What is your opinion?
Warning [2] Illegal string offset 'title' - Line: 66
if(!isset($params['title'])) {
$params['title'] = "Hidden Content";
}
and Warning [2] Illegal string offset 'title' - Line: 3
eval("\$return = \"".$templates->get("lock_wrapper")."\";");
Please help!
I got this error on my website this morning when I click on post-ad
I have tried looking at the code but don't seem to find anything wrong
if (!function_exists('adforest_extarct_link')) {
function adforest_extarct_link($string) {
$arr = explode('|', $string);
list($url, $title, $target, $rel) = $arr; /* This is line 148 */
$rel = urldecode(adforest_themeGetExplode($rel, ':', '1'));
$url = urldecode(adforest_themeGetExplode($url, ':', '1'));
$title = urldecode(adforest_themeGetExplode($title, ':', '1'));
$target = urldecode(adforest_themeGetExplode($target, ':', '1'));
return array("url" => $url, "title" => $title, "target" => $target, "rel" => $rel);
}
This is the error Message
Undefined offset: 3 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148
It actually has 3 lines of error:
Notice: Undefined offset: 1 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148
Notice: Undefined offset: 2 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148
Notice: Undefined offset: 3 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148
Question is broadly a duplicate of PHP undefined offset from list()
However,
Your list expects at least 4 prameters -- but your $arr array only has 1. So the following three are empty. (remember arrays start at 0). So your $string does not contain a | character for the explode function to work as intended.
Workaround:
Original:
$arr = explode('|', $string);
list($url, $title, $target, $rel) = $arr; /* This is line 148 */
Becomes:
$arr = array_pad(explode('|', $string), 4, null);
list($url, $title, $target, $rel) = $arr;
What this does:
Pads the array out to contain a minimum of 4 values; so that the list values will always be populated, even if they may still be empty.
This question already has answers here:
Illegal String Offset within PDO For Each loop
(2 answers)
Closed 8 years ago.
After completing a SELECT query on a MySQL database and applying the fetchAssoc() method to the result, I am receiving warnings when attempting to access the results in a foreach loop. Here is the code:
$query=$subquery->execute()->fetchAssoc();
foreach($query as $result) {
if ($result['active'] == 'Y'){ // (this is line 703)
$page_id = $result['page_id']; // (this is line 704)
if (!$package_pages[$page_id] || $package_pages[$page_id] != $page_id) { // line 705
$pages[] = $page_id;
}
}
I inserted var_dump($query) to inspect the results of the query. Here is an example of the output:
array (size=3)
'page_name' => string 'Apts & Rentals' (length=14)
'page_id' => string '49' (length=2)
'active' => string 'Y' (length=1)
array (size=3)
'page_name' => string 'Homepage' (length=8)
'page_id' => string '1' (length=1)
'active' => string 'Y' (length=1)
There are 25 arrays output from var_dump($query)
And here is a sample of the warnings:
Warning: Illegal string offset 'active' in ads_load() (line 703 of ...
Warning: Illegal string offset 'page_id' in ads_load() (line 704 of ...
Notice: Undefined index: Y in ads_load() (line 705 of ...
Why are the offsets 'active', and 'page_id' being flagged as illegal?
Where is the "Notice: Undefined index: Y" coming from since it is not being used as an index on line 705?
I don't believe this is a duplicate of Illegal String Offset within PDO For Each loop. The issue was resolved when I realized the code given above was placed within another loop which gave the impression I was dealing with an array of arrays rather than a single array each time.
You're accessing them incorrectly - you've got an associative array and you're trying to loop through it as if it were an array of associative arrays. This is your dumped data structure:
$query = array(
'page_name' => 'Homepage',
'page_id' => '1',
'active' => 'Y' );
You can get the results directly from $query, i.e.:
$query=$subquery->execute()->fetchAssoc();
if ($query['active'] == 'Y')
$page_id = $query['page_id'];
// ... the rest of your code ...
}
You could iterate through the keys and values of $query, but since you are interested in particular values from the $query array, there isn't much point.